Tag: 排序

使用链表在C中插入排序

我必须制作电话目录程序。 程序应该从文件中读取名称和数字。 我已成功创建包含此数据的链接列表。 现在我想按字母顺序对它们进行排序。 我该怎么做?

使用备用最小值和最大值对数组进行排序

给定一个数组,我被要求以某种方式对其进行排序,以便最小值首先是最大值第二,第二个最小值第三,依此类推。 但是,当我输入值时,我没有得到所需的输出。 我今天参加考试,对此表示赞赏。 #include int main() { int i,j,k,a[6],temp,min; for(i=0;i<6;i++) scanf("%d",&a[i]); for(j=0;j<6;j++) { if(j%2==0) { min=a[j]; for(k=j;k<6;k++) { if(a[k++]<min) min=a[k++]; } temp=a[j]; a[j]=min; min=temp; } else { min=a[j]; for(k=j;kmin) min=a[k++]; } temp=a[j]; a[j]=min; min=temp; } printf(“%d “,a[j]); } }

C – 合并合并排序的一部分

我是新来合并排序,我正在尝试创建一个。 我的合并排序不是排序我发送它的数组,我无法弄清楚为什么。 这里是所有代码http://pastebin.com/M4RUzhUa的链接 这是我的mergesort函数 void merge_sort(int array[], int low, int high) { int middle = (low + high) / 2; if(low < high) { merge_sort(array, low, middle); merge_sort(array, middle+1, high); merge(array, low, middle, high); } } 这是我的(更新的)合并function void merge(int array[], int low, int middle, int high) { int size,left,right,i, j; size = high – low […]

如何改进基数排序的这种实现?

我正在实现一个2字节的基数排序。 概念是使用Counting Sort,对整数的低16位进行排序,然后对高16位进行排序。 这允许我在2次迭代中运行排序。 我的第一个概念是试图找出如何处理否定。 由于符号位将被翻转为负数,然后以hexforms,这将使负数大于正数。 为了解决这个问题,我在符号位为正时将其翻转,以使[0,2 bil] = [128 000 000 000,255 255 …)。 当它是负数时,我将所有位翻转,使其范围为(000 000 ..,127 255 ..)。 这个网站帮助我了解这些信息。 为了完成它,我会根据传递将整数分成顶部或底部16位。 以下是允许我这样做的代码。 static uint32_t position(int number, int pass) { int mask; if (number > 31) | 0x80000000; uint32_t out = number ^ mask; return pass == 0 ? out & 0xffff : (out >> […]

在C中,如何对每个指针指向int的可变长度数组的指针数组进行排序?

我的问题是如何通过所有其他数组中的第一个数字对arr6进行排序。 我的意思是,如果arr1在第一个num有3个,那意味着它需要在arr6为3。 最后, arr6需要首先指向arr4 , arr4 , arr2 , arr1 , arr5 。 #include #include void sort_arr(int **arr6); void print_arr(int **arr6); void order_arr(int **arr6); int main(void) { int i = 1; int arr1[] = { 3, 9, 6, 7 }; int arr2[] = { 2, 5, 5 }; int arr3[] = { 0 }; int […]

我想使用链表快速排序

编写一个函数,通过引用获取两个学生记录结构,并交换除下一个指针之外的所有内容。 使用您的函数实现冒泡排序算法以对链表进行排序(不要使用数组)。 #include #include #include /*defined a structure for date of birth*/ struct birth{ int date; int month; int year; }; struct studentrecord{ char name[64]; struct birth dob; int height; float weight; struct studentrecord *next; struct studentrecord *prev; }; struct studentrecord* printlist(struct studentrecord* p){ if(p==NULL) return 0; printf(“%s\t%2d%2d%4d\t%d\t%.2f\n”,p->name,(p->dob).date,(p->dob).month,(p- >dob).year,p->height,p->weight); printlist(p->next); } /*swapped all the contents […]

qSort not Sorting my array

这是我的代码: #include #include float comp (const void * elem1, const void * elem2) { float f = *((float*)elem1); float s = *((float*)elem2); if (f > s) return 1; if (f < s) return -1; return 0; } int main(void) { int t, n, temp, temp1, x; float input[2][50][1000]; scanf("%d", &t); for(temp=0; temp<t; temp++){ scanf("%d ", […]

基于第一列排序二维数组

我有一个temp2.dat文件: 0.060493 1 0.5 1 1.596961 0 0.1 2 0.87758 1 0.3 1.5 0.165453 1 0 3 0.07085 1 0.3 4 0.125379 1 0.2 3 0.454202 1 0.2 2 0.373227 1 0.3 1 0.131486 1 0.3 3 0.867477 0 0.5 4 0.122609 0 0.8 9 现在我想在C编写函数,以递增的顺序对这4列进行排序,但仅基于第一列的值。 我尝试修改以下代码但失败了: struct data_t { double x; int y; double […]

冒泡排序Carrays交换

当我到达此代码的交换部分时,我最近遇到了这个问题,所以这段代码的作用是输入一个数组并使用冒泡排序方法对其进行排序。 读取此内容的文本文件有10个数字和名称。 像这样: 约翰1 马克2 马太福音2 路加福音3 伊萨克4 凯恩5 瑞恩7 亚伯2 亚当9 夏娃10 但是当它打印出来时,它会显示: 约翰1 马克2 马太福音2 亚伯2 亚伯3 亚伯4 亚伯5 亚伯7 亚当9 夏娃10 对不起,问题是为什么它会重复Abel,我该怎么做才能解决它? void bubbleSort (Word q){ int last = 9; int Swapped = 1; int i = 0; int m = 0; char* temp = “Hello”; printf(“Temp is: %s \n”, temp); int […]

选择使用结构数组在C中排序,错误:“需要左值”

试图对一组Structs进行排序。 Struct是下面定义的TextArt typedef struct //struct that holds ASCII art { char artistName[80]; //name of artist char asciiArt[20][80]; //actual ascii art line by line int rating; //rating of art }TextArt; 我不认为结构与此有任何关系。 我得到编译器错误 错误:当尝试将一个结构分配给另一个结构时,左值作为赋值的左操作数(见下文) temp = asciiArt+pos_min; asciiArt+pos_min = asciiArt+i; //error here asciiArt+i = *temp; //error also here 呼吁运作 selectionSort(artPtr, artArrSize); 和全选择排序function。 有什么我不明白用C =在C中分配结构吗? 我以为是这个或者我对TextArt数组的传递在某种程度上是错误的。 请赐教,谢谢。 void […]