bubble-sort

Bubble sort using only pointers and dynamic memory allocation

最后都变了- 提交于 2019-12-13 07:48:34
问题 I am trying to run Bubble Sort Technique using pointers and dynamic memory allocation, but the code doesn't seem to run (eclipse crashes). Please help. I am posting the code below: #include<iostream> using namespace std; void sort(int *); //=========================================== int main() { int *a = new int[5]; int *c = a; cout << "Enter the numbers\n"; for(int i = 0; i < 5; i++) { cin >> *a; a++; } a = a - 4; sort(a); cout << c; cout<<"\nSorting complete"; cout<<"\n Array after sorting

Segmentation Fault in Bubble Sort

断了今生、忘了曾经 提交于 2019-12-13 06:18:50
问题 Trying to write a bubble sort algorithm which sorts any data type and works similar to qsort in stdlib in C. This is the Code ive written, compiling it gives me a "Segmentation Fault" error Tried compiling with -g for gdb debugging which gave way more errors as: In function `testcmp': (.text+0x21a): multiple definition of `testcmp' /tmp/cc9ULHuO.o:new.c:(.text+0x12d): first defined here as: In function `_fini': (.fini+0x0): multiple definition of `_fini' /usr/bin/ld: /usr/lib/debug/usr/lib

Sorting a 2D Char Array in Alpha order?

二次信任 提交于 2019-12-13 04:43:05
问题 I am trying to sort a 2D array of names into alphabetical order, but I can not seam to get it to work. I am using a bubble sort on the letters, and this is sorting the 1st letter of the names fine, but 3 of the names start with the same letter and they are still out of order. I have tried googleing and stuff but every ting says to use vectors or string variables.. but I am limited to using 2d char arrays.. Any ideas? Here is the code I have at the moment that works nearly: using namespace std

Bubble sort in Prolog language

北战南征 提交于 2019-12-13 04:06:23
问题 I must implement the bubble sort function (the sorting algorithm). I have already implemented bubblesort and swap , a help function for bubblesort : swap([X,Y|T1],[Y,X|T1]):-(Y<X,!). swap([X|T1],[X|T2]):- swap(T1,T2). bubblesort([],[]) :- !. bubblesort(T1,T2) :- (bubblesort(swap(T1,T2),T2)). I get an infinite loop. I must keep the signature of the function: bubblesort(T1,T2) I'm stuck on this question for 2 hours. Does anyone have an idea how I can do that? 回答1: The problem was caused by

No Match for Operator[]

北城以北 提交于 2019-12-13 03:36:18
问题 So I'm trying to use a sorting function (similar to bubble) and pass into it an object. If that object is bigger (alphabetically) then switch then return true and switch that with the before it. I keep getting an error though inside the if statement inside mySort() which says "no match for operator[] in arr[j]" but from my understanding I'm passing an object array right? Why is this happening and how can I solve it? Here's the driver #include <iostream> #include <fstream> #include <string>

How do I get another counter?

≯℡__Kan透↙ 提交于 2019-12-13 02:06:53
问题 I'm trying to compare bubble sort between C++ and MASM. I've got the C++ working without issue. With MASM, however, I need another counter in loopSwap, but I don't know how to go about it. I know that if I push a register, it would have to be before the comparison but if the comparison jump is met, I wouldn't be able to pop the same register. Any help is appreciated! C++ Code: #include <iostream> #include <cmath> #include <ctime> using namespace std; int deepBlueDecend(int* num, int size);

Parallel Bubble sort on GPU

Deadly 提交于 2019-12-12 20:39:46
问题 i am implementing the simple bubble sort algorithm using CUDA, and i have a question. i perform the following code in order to swap 2 consecutive elements in the array: if(a[threadIdx.x]>a[threadIdx.x + 1]) Swap(a[threadIdx.x] , a[threadIdx.x + 1]); note that the number of threads in the block is half the size of the array. Is this a good implementation? would threads in a single warp execute in parallel even if there is a branch? therefore it would actually take N iterations in order to sort

Bubble sorting with one list comprehension

一世执手 提交于 2019-12-12 10:24:42
问题 I'd like to see if it would be possible to convert a BubbleSort function, such as: def BubbleSort(l): for i in range(len(l)-1): for j in range(len(l)-1-i): if (l[j]>l[j+1]): l[j],l[j+1]=l[j+1],l[j] return l to a one-liner list comprehension, maybe similar to: def BubbleSort(l): return [something_goes_here for i in range(len(l)-1) for j in range(len(l)-1-i) if (l[j]>l[j+1])] Sample Input: print(BubbleSort([1,5,-5,0,10,100])) Sample Output [-5, 0, 1, 5, 10, 100] 回答1: A solution based on side

Efficency of Insertion Sort vs Bubble sort vs Selection sort?

こ雲淡風輕ζ 提交于 2019-12-12 07:24:48
问题 I have written down that Insertion Sort is faster than Selection Sort, which is faster than Bubble Sort, and that their running time for all 3 are O(n^2), but what can I say to compare them with each other? 回答1: You can compare sorting algorithms against the following criteria: Time Complexity (Big-O notation). You should note that best-case, worst-case and average run-time can have different time complexity. For example best-case for Bubble Sort is only O(n), making it faster than Selection

Swap Position of the Node in C

ⅰ亾dé卋堺 提交于 2019-12-12 06:00:04
问题 Okay so I want to swap POSITION (not values) of two nodes. My Program is running with any errors or warnings, but I am not sure if I am swapping position or values. Here is my sort function: void sort(struct node **recordsHead,int (*compare_fcn)(struct node*, struct node*)) { void swap(struct node**, struct node**); struct node *tmp,*lastPtr = NULL; int swapped; do { swapped = 0; tmp = *recordsHead; while (tmp->next_ != lastPtr) { if (compare_fcn(tmp, tmp->next_)) { swap(&tmp, &(tmp->next_));