Tag: 排序

输出以奇怪的方式排序

我写下面的代码,但从输出中看到的是错误的。 我可能犯了一个指针错误。 你能帮我吗? 未排序的名称: 纽约乔治亚波士顿 排序名称: Bostork Georgia Newyon #include #include #include #define SIZE 3 void sort(char x[3][100]); void swap(char **, char **); int i,j; char names[SIZE][100]; char *temp; int main(void){ //get the names of the cities puts(“Enter names of cities”); for (i = 0; i < SIZE; i++) { fgets( names[i], 99, stdin ); […]

循环创建双精度并将它们插入到排序数组中,在C中

假设我有一组已排序的双打。 { 0.124, 4.567, 12.3 } 一个正的,非零的double是由代码的另一部分创建的,需要在保持排序的同时插入到该集合中。 例如,如果创建的double是7.56 ,则最终结果是, { 0.124, 4.567, 7.56, 12.3 } 在我的代码中,这个“创建双重并插入有序集合”过程然后重复了很多次。 可能是500k到100万次。 我不知道总共会创造多少双打,但我知道上限。 尝试 我天真的第一种方法是创建一个长度=上限的数组,并用零填充它,然后添加初始的双精度集(“add”=用双精度替换0值的数据)。 每当创建一个double时,我将它添加到数组并执行插入排序,我读到这对排序有序数组很有用。 题 我有一种感觉,运行500k到100万插槽将是一个严重的性能问题。 (或者我错了?)在C中是否有更高效的数据结构和/或算法? 编辑: 我想保持集合排序的原因是因为在每次“创建双重并插入有序集合”过程之后,我需要能够查找该集合中的最小元素(并且可能通过将其替换为0来删除它) )。 我认为这样做的最好方法是保持集合排序。 但如果情况并非如此,也许还有另一种选择?

带链接列表的快速排序

我写下了以下程序,该程序使用快速排序算法来排序使用链接列表将多少整数放入命令行。 我不仅得到关于混合声明的ISO C90错误,而且我的代码中某处存在内存泄漏,我不知道如何修复它。 任何帮助,将不胜感激! #include #include “linked_list.h” #include #include “memcheck.h” #include #include node *quicksort(node *list); int ListLength (node *list); int main(int argc, char *argv[]) { if (argc == 1) { fprintf(stderr, “usage: %s [-q] number1 number2 … \ (must enter at least one argument)\n”, argv[0]); exit(1); } node *list; node *sorted_list; int i; int intArg […]

如何按C中的优先级对列表项进行排序?

我想按用户键入的优先级对列表项进行排序,并且它做得很好。 但是,当有多个具有相同优先级的项目时,它不会按照预期的到达顺序对它们进行排序。 对不起,如果我说的不够清楚你可以理解。 变量的名称是葡萄牙语,所以如果您不理解,请询问。 这是代码: typedef struct pedido pedido, *ppedido; struct pedido{ char id[5]; int prioridade; int mesa, n_pratos; struct prato *prato[TAM]; ppedido prox; }; struct prato{ char id[5]; }; ppedido novo_pedido(ppedido lista) { ppedido novo, aux, anterior = NULL; int i; novo = (struct pedido*)malloc(sizeof(pedido)); if(novo == NULL){ printf(“Erro na alocacao de memoria…\n”); return; […]

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

我正在研究一个项目,它以几种不同的方式对指针数组中的数组进行排序,但我仍然坚持一种排序方式。 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, […]

在一个函数中对5个数组进行排序

如果我有5个数组和一个包含所有5个数组的指针数组,我需要编写一个只使用指针数组对每个数组进行排序的函数,我该怎么做? 该函数需要从索引1(!)开始对数组中的每一个进行排序,而不是0。 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[] = { arr1, arr2, arr3, arr4, arr5 }; 我知道如何对一个数组进行排序,但是当我尝试以最有效的方式使用指针数组对每一个数组进行排序时,我有点失落。 也许有一个选项可以在一个循环中对每个数组进行排序? […]

从链表中排序元素

void sortlist() { struct node *a; struct node *temp=head; struct node *temp1=head->next; while(temp!=NULL) { while(temp1->next!=NULL) { if(temp->data > temp1->data) { a->data=temp->data; temp->data=temp1->data; temp1->data=a->data; } else { temp1=temp1->next; } } temp=temp->next; } } //我是数据结构的新手。我在尝试对链接列表的元素进行排序时遇到了一些问题。列表没有得到排序。非常感谢任何帮助。

使用qsort函数以交替方式对整数数组进行排序。

/ *我最近学习了qsortfunction。 这个c代码给出了错误的输出。 需要帮助。 问题 – 以交替方式对整数数组进行排序。 (偶数指数的元素和奇数指数的元素分别排序)OUTPUT- 0 4 1 2 5 8 7 5 9 3 10 5 * / #include #include // This function is used in qsort to decide the relative order // of elements at addresses p and q. int comparator(const void *p, const void *q) { // Get the […]

根据长度崩溃对静态数组中的字符串进行排序? |错误的分配/访问|

我想创建一个字符串数组,并根据它们的长度(最小 – >最大)按顺序对它们进行排序,但程序在所有输入后崩溃。 并且它绕过元素0(在输入期间直接从元素1开始) #include #include main() { int i,j,N; printf(“\nInput amount of alphanumericals: “); scanf(“%d”,&N); { int min; char *swap=(char*)malloc(sizeof(char)*150); char *A[N],**temp; for(i=0;i<N;i++) *(A+i)=malloc(sizeof(char)*N);//Error Here temp=A; for(i=0;i<N;i++){ printf("\nInput %d element:",i+1); fgets(temp+i,150,stdin);//And Here } printf("\n\nData ["); for(i=0;i<N;i++) printf(" %s",A[i]); printf(" ]\n\n"); //insertion sort for(i=0;i<N;i++){ min=i; for(j=i+1;j<N;j++){ if(strcmp(A[j],A[min])<0){ min=j; } } if(min!=i){ swap=A[i]; A[i]=A[min]; A[min]=swap; } […]

如何在c / c ++中按字母顺序将字符串中的字母排序?

大家好,在我编写AaBc的时候,它编写了’ABab’。 我应该在这段代码中改变什么,如果我想要它像’AaBb’那样写? #include #include #include int main (void) { char string[128], temp; int n, i, j; printf(“\nEnter string: “); gets(string); n = strlen(string); for (i=0; i<n-1; i++) { for (j=i+1; j string[j]) { temp = string[i]; string[i] = string[j]; string[j] = temp; } } } printf(“\n%s”, string); printf(“\n”); return 0; }