Tag: bubble sort

冒泡排序中的分段错误

试图编写一个冒泡排序算法,对任何数据类型进行排序,其工作方式类似于C中stdlib中的qsort。 这是我编写的代码,编译它给我一个“分段错误”错误尝试使用-g编译gdb调试,这让错误更多 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/x86_64-linux-gnu/crti.o(.debug_info): relocation 0 has invalid symbol index 7 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 1 has invalid symbol index 8 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_info): relocation 2 has invalid symbol index 9 /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crti.o(.debug_ranges): relocation 0 […]

如何使用气泡排序或选择排序按降序对指针数组中的数组进行排序?

我正在研究一个项目,它以几种不同的方式对指针数组中的数组进行排序,但我仍然坚持一种排序方式。 arrays的构建方式是第一个数字表示后面的数字量。 例如,(3,0,23,1):此数组在第一个索引后有3个数字)。 我想从最低到最高的数字对数组进行排序, 但我不想更改第一个索引,这意味着数组看起来像这样(3,0,1,23)。 这些是数组和指针数组: int arr1[] = { 3, 9, 6, 7 }; int arr2[] = { 2, 5, 5 }; int arr3[] = { 0 }; int arr4[] = { 1, 6 }; int arr5[] = { 4, 5, 6, 2, 1 }; int * pArr[SIZE] = { arr1, arr2, arr3, arr4, […]

冒泡排序双链表

我的双链表的泡泡分拣function有问题。 当我以单链接的方式对节点进行排序时(仅使用 – > next),它正在工作,但我无法使用 – > prev指针。 这是我正在使用的代码: void sort(int count) { struct data *tmp,*current,*nextone; int i,j; for(i=0;i<count;i++) { current = first; for(j=0;jnumber > current->next->number) { nextone = current->next; current->next = nextone->next; nextone->next = current; if(current == first) { first = nextone; current = nextone; } else { current = nextone; tmp->next = nextone; […]

C OpenMP并行冒泡排序

我在C中使用OpenMP实现了并行冒泡排序算法( Odd-Even转置排序 )。 然而,在我测试它之后,它比串行版本慢了(大约10%),尽管我有一个4核处理器(2个真正的x 2,因为英特尔超线程)。 我已经检查过核心是否实际使用过,我可以在运行程序时以100%的比例看到它们。 因此我认为我在实现算法时犯了一个错误。 我使用linux与内核2.6.38-8-generic。 这是我编译的方式: gcc -o bubble-sort bubble-sort.c -Wall -fopenmp或 gcc -o bubble-sort bubble-sort.c -Wall -fopenmp用于串行版本 这就是我的运行方式: ./bubble-sort out_10000 #include #include #include #include int main() { int i, n, tmp, *x, changes; int chunk; scanf(“%d “, &n); chunk = n / 4; x = (int*) malloc(n * sizeof(int)); for(i = […]

使用C中的指针冒泡排序结构

我想使用冒泡排序算法和C中的指针对结构数组进行排序。我有一个汽车结构: typedef struct{ char model[30]; int hp; int price; }cars; 我为12个项目分配内存: cars *pointer = (cars*)malloc(12*sizeof(cars)); 并从文件中读取数据: for (i = 0; i model, &(pointer+i)->hp, &(pointer+i)->price); } 我将指针ptr传递给bubbleSort函数: bubbleSort(pointer, number); 这是我的bubbleSort函数: void bubbleSort(cars *x, int size) { int i, j; for (i=0;i<size-1;i++) { int swapped = 0; for (j = 0; j hp > (x+j+1)->hp ) { […]

如何使此函数采用任意字符串?

所以基本上,现在,这个函数只能用9个单词,每个10个字符。 如何制作它以便它可以采用arbitrary数量的words和characters并按字母顺序对其进行相应的排序? int sortText(){ char name[10][9], tname[10][9], temp[10]; int i, j, n; printf(“Enter the amount of words you want to sort (max 9):”); scanf(“%d”, &n); printf(“Enter %d words: “,n); for (i = 0; i < n; i++) { scanf("%s", name[i]); strcpy(tname[i], name[i]); } for (i = 0; i < n – 1 ; i++){ for […]

C中的冒泡排序算法

我试图完成的程序是一个使用冒泡排序算法的程序。我不确定问题是什么或问题在哪个函数中。问题是程序没有正确排序数组。(它也必须按升序排列)。 这是代码: #include #include “simpio.h” void getArray (int arr[], int size); void sortArray (int arr[], int size); void swap (int arr[], int num, int number); void dispArray (int arr[], int size); bool checkBigger (int arr[], int num, int number); main() { int size; printf(“Enter number of elements: “); size=GetInteger(); int arr[size]; getArray(arr, size); sortArray(arr, size); […]

bubble在c中按字母顺序对字符数组进行排序

我正在尝试按字母顺序对字符数组进行冒泡。 我的代码如下: #define CLASS_SIZE 10 #include void bubbleSortAWriteToB(const char a[], char *b[]); int main(void){ char *s_letters[CLASS_SIZE]; char letters[CLASS_SIZE] = {‘a’,’r’,’p’,’b’,’r’,’c’,’x’,’e’,’w’,’j’}; bubbleSortAWriteToB(letters,s_letters); return 0; } void bubbleSortAWriteToB(const char a[], char *b[]){ char temp; int i,j; for(i=0;i<CLASS_SIZE-1;i++){ for(j=1;j(int)a[j]){ temp = a[j]; *b[j] = a[j-1]; *b[j-1] = temp; } } } } 它没有给出任何类型的错误,但是当我运行它时它会像在inifinte循环中那样陷入困境。 但从我所看到的情况来看也不是这样。 你能帮我吗?