知识点总结报告 1.26

知识点:排序

在C语言中,对一组数据进行排序有多种方法:交换排序、选择排序、冒泡排序、插入排序、希尔排序、归并排序、以及快速排序等

其中冒泡排序和交换排序比较简单,交换排序是又是选择排序的基础。以下这个例子包含几种排序方法,以对分数降序排序为背景,使用链表,顺便填补对链表中数据排序的空白。

  1 #include <stdio.h>
  2 #include <malloc.h>
  3 #include <stdlib.h>
  4 struct link
  5 {
  6     int data;
  7     struct link *next;
  8 };
  9 void DisplayNode(int *pData,int n);
 10 struct link *AppendNode(struct link *head,int *pData);
 11 void ExchangeSort(int n,int *pData);
 12 void SelectionSort(int n,int *pData);
 13 void bubbleSort(int n,int *pData);
 14 void InsterSort(int n,int *pData);
 15 void ShellSore(int n,int *pData);
 16 void QuickSort(int n,int *pData);
 17 int main()
 18 {
 19     int n,i;
 20     int *pData=NULL;//定义一个基类型为整形的指针数组,用于指向节点数据域,方便排序
 21     struct link *head=NULL;
 22     printf("How many student?\n");
 23     scanf("%d",&n);
 24     pData=(int *)malloc(n*sizeof(int));
 25     for(i=0; i<n; i++)
 26     {
 27         printf("Input %d student's score\n",i+1);
 28         head=AppendNode(head,pData);
 29     }
 30     DisplayNode(pData,n);
 31     //ExchangeSort(n,pData);
 32     //SelectionSort(n,pData);
 33     //bubbleSort(n,pData);
 34     //InsterSort(n,pData);
 35     //ShellSore(n,pData);
 36     QuickSort(n,pData);
 37     DisplayNode(pData,n);
 38     return 0;
 39 }
 40 
 41 struct link *AppendNode(struct link *head,int *pData)
 42 {
 43     int i=1;
 44     int score;
 45     scanf(" %d",&score);
 46     struct link *p=NULL,*pr=head;
 47     p=(struct link *)malloc(sizeof(struct link));
 48     if(p==NULL)
 49     {
 50         printf("No enough memory\n");
 51         exit(0);
 52     }
 53     if(head==NULL)
 54     {
 55         head=p;
 56         p->data=score;
 57         p->next=NULL;
 58         pData[0]=p->data;
 59     }
 60     else
 61     {
 62         while(pr->next!=NULL)
 63         {
 64             pr=pr->next;
 65             i++;
 66         }
 67         pr->next=p;
 68         p->data=score;
 69         p->next=NULL;
 70         pData[i]=p->data;
 71     }
 72     return head;
 73 }
 74 
 75 void DisplayNode(int *pData,int n)
 76 {
 77     int i;
 78     for(i=0;i<n;i++)
 79     {
 80         printf("%4d",pData[i]);
 81     }
 82     printf("\n");
 83 }
 84 
 85 void ExchangeSort(int n,int *pData)
 86 {
 87     int i,j,temp,count=0;
 88     for(i=0;i<n-1;i++)
 89     {
 90         for(j=i+1;j<n;j++)
 91             if(pData[j]>pData[i])
 92         {
 93             temp=pData[i];
 94             pData[i]=pData[j];
 95             pData[j]=temp;
 96             count++;
 97         }
 98     }
 99     printf("\nExchange times:%d\n",count);
100 }
101 
102 void SelectionSort(int n,int *pData)
103 {
104     int i=0,j=0,k,temp=0,count=0;
105     for(i=0;i<n-1;i++)
106     {
107         k=i;
108         for(j=i+1;j<n;j++)
109         {
110             if(pData[k]<pData[j])
111                 k=j;
112         }
113         if(i!=k)
114         {
115             temp=pData[k];
116             pData[k]=pData[i];
117             pData[i]=temp;
118             count++;
119         }
120     }
121     printf("Selection times %d\n",count);
122 }
123 
124 void bubbleSort(int n,int *pData)
125 {
126     int i,j,temp=0;
127     for(i=0;i<n;i++)
128     {
129         for(j=i+1;j<n;j++)
130         {
131             if(pData[i]<pData[j])
132             {
133                 temp=pData[i];
134                 pData[i]=pData[j];
135                 pData[j]=temp;
136             }
137         }
138     }
139 }
140 
141 void InsterSort(int n,int *pData)
142 {
143     int i,j,temp;
144     for(i=0;i<n-1;i++)
145     {
146         for(j=i+1;j>0;j--)
147         {
148             if(pData[j]>pData[j-1])
149             {
150                 temp=pData[j];
151                 pData[j]=pData[j-1];
152                 pData[j-1]=temp;
153             }
154             else
155             {
156                 break;
157             }
158         }
159     }
160 }
161 
162 void ShellSore(int n,int *pData)
163 {
164     int i=0,j=0,k=0,incre=n,temp=0;
165     while(incre!=1)
166     {
167         incre=incre/2;
168         for(k=0;k<incre;k++)
169         {
170              for(i=k+incre;i<n;i+=incre)
171              {
172                  for(j=i;j>k;j-=incre)
173                 {
174                     if(pData[j]>pData[j-incre])
175                     {
176                         temp=pData[j];
177                         pData[j]=pData[j-incre];
178                         pData[j-incre]=temp;
179                     }
180                     else
181                     {
182                         break;
183                     }
184                 }
185              }
186         }
187     }
188 }
189 
190 void QuickSort(int n,int *pData)
191 {
192     int i,j,p,temp;
193     if(n<2)
194         return ;
195     p=pData[n/2];
196     for(i=0,j=n-1;;i++,j--)
197     {
198         while(pData[i]>p)
199             i++;
200         while(p>pData[j])
201             j--;
202         if(i>=j)
203         {
204             break;
205         }
206         temp=pData[i];
207         pData[i]=pData[j];
208         pData[j]=temp;
209     }
210     QuickSort(i,pData);
211     QuickSort(n-i,pData+i);
212 }

 题目:http://acm.hdu.edu.cn/showproblem.php?pid=1106

知识点:插入排序

分析:因为这道题给出的一组数已经排好序了,只需按升序排序插入一个数,所以插入的时候一个循环就可以,很简单。

题解:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 void Display(int arr[],int m);
 4 int main()
 5 {
 6     int i,j,k,temp=0;
 7     int arr[101]= {0};
 8     int m,n;
 9     while(scanf("%d %d",&m,&n) !=EOF)
10     {
11         if(m==0&&n==0)
12             exit(0);
13         arr[m]=n;
14         for(k=0;k<m;k++)
15         {
16             scanf("%d",&arr[k]);
17         }
18         for(i=m;i>0;i--)
19         {
20             if(arr[i]<arr[i-1])
21             {
22                 temp=arr[i];
23                 arr[i]=arr[i-1];
24                 arr[i-1]=temp;
25             }
26             else
27             {
28                 break;
29             }
30         }
31         for(j=0;j<m;j++)
32         {
33             printf("%d ",arr[j]);
34         }
35         printf("%d",arr[m]);
36         printf("\n");
37     }
38     return 0;
39 }

但注意输出格式,否则很容易"Presentation Error".

posted @ 2018-01-27 23:40  给我的敌人开黑市  阅读(122)  评论(0编辑  收藏  举报