09-Sort 1, 09-Sort 1
As an electronic major student, the C language teaching material I use is important and difficult to sort bubbles. After reading this question, I know that it is just a piece of cake, the Bubble sorting test points 4, 6, and 8 times out, the insertion Sorting Test Point 6 times out, and finally the hill sorting is used. The test points are all over when the original incremental sequence is used.
1 #include <stdio.h> 2 //#include <stdbool.h> 3 4 //void Bubble_Sort(int * a, int n); 5 //void Insert_Sort(int * a, int n); 6 void Shell_Sort(int * a, int n); 7 8 int main() 9 {10 int N;11 scanf("%d", &N);12 int a[N];13 14 for(int i = 0; i < N; i++)15 {16 scanf("%d", &a[i]);17 }18 // Bubble_Sort(a, N);19 // Insert_Sort(a, N);20 Shell_Sort(a, N);21 22 return 0;23 }24 25 //void Bubble_Sort(int * a, int n)26 //{27 // int i, j;28 // bool flag;29 // 30 // for(i = n - 1; i > 0; i--)31 // {32 // flag = true;33 // for(j = 0; j < i; j++)34 // {35 // if(a[j] > a[j + 1])36 // {37 // flag = false;38 // a[j] += a[j + 1];39 // a[j + 1] = a[j] - a[j + 1];40 // a[j] -= a[j + 1];41 // }42 // }43 // if(flag)44 // break;45 // }46 // for(i = 0; i < n; i++)47 // {48 // printf("%d", a[i]);49 // if(i != n - 1)50 // printf(" ");51 // else52 // printf("\n");53 // }54 //}55 56 //void Insert_Sort(int * a, int n)57 //{58 // int i, j, tmp;59 // 60 // for(i = 1; i < n; i++)61 // {62 // tmp = a[i];63 // for(j = i; j > 0 && a[j - 1] > tmp; j--)64 // a[j] = a[j - 1];65 // a[j] = tmp;66 // }67 // for(i = 0; i < n; i++)68 // {69 // printf("%d", a[i]);70 // if(i != n - 1)71 // printf(" ");72 // else73 // printf("\n");74 // }75 //}76 77 void Shell_Sort(int * a, int n)78 {79 int i, j, k, tmp;80 for(i = n / 2; i > 0; i /= 2)81 {82 for(j = i; j < n; j++)83 {84 tmp = a[j];85 for(k = j; k >= i && a[k - i] > tmp; k -= i)86 a[k] = a[k - i];87 a[k] = tmp;88 }89 }90 for(i = 0; i < n; i++)91 {92 printf("%d", a[i]);93 if(i != n - 1)94 printf(" ");95 else96 printf("\n");97 }98 }
The questions are as follows:
If N integers (within the range of long integers) are specified, the results of sorting from small to large must be output.
This question aims to test the performance of different sorting algorithms in various data scenarios. The test data of each group has the following characteristics:
- DATA 0: only one element;
- The test is basically correct if the data contains an integer;
- The data is a random integer;
- The data is a random integer;
- The data is 4: Random integers;
- The data is an ordered integer;
- The data is an integer in the descending order;
- The data is an integer with a basic order;
- The data is a random positive integer at. Each number cannot exceed 1000.
Input Format:
The first line of the input is a positive integer N (<= 105), followed by N integers in the Long Integer Range, which are separated by spaces.
Output Format:
Output results in ascending order of a row. Numbers are separated by one space. No extra space is allowed at the end of the row.
Input example:
114 981 10 -17 0 -20 29 50 8 43 -5
Output example:
-20 -17 -5 0 4 8 10 29 43 50 981