Sorting of C++ Learning (C Language Part) (Bubble Selection Insert Quick Row)

Keywords: C++ less

Algorithms are solutions to a class of problems
Sorting algorithm ranks from small to large and from large to small according to the size relationship of elements
Bubble Selection Insert Quick Row
Hill sort merge sort heap sort

Bubble sorting compares the top to bottom or the bottom to float the smallest number in each round

Choose Sort 1. Find the smallest/largest number and put it at the top 2. Find the smallest/largest number and the second element to swap only once for each round, except for the first number.

Insertion sort
Suppose 1 259 0
(1) Now 1 is an orderly choice, 5 is compared with 1, bigger is put in front of 1, smaller is put behind 1.
Later elements are inserted one by one into the previous queue to ensure orderly insertion
After the final insertion, the sequence of this element is also ordered.
Quick sort

General sorting time complexity and small space complexity will be higher
Space complexity is small and time complexity is high

Time space for space or space for time
--> Either swap time for memory or swap memory for time

 

The test code notes are as follows:

  1 #include<stdio.h>
  2 
  3 //Split a function to implement 1.Sort Array 2.The element format in the array determines the number of sorting times---->parameter
  4 
  5 //Bubble sort
  6 void bullet_sort(int*arr,int len)  //perhaps int*arr  It's the same.  int len Is the array size
  7 {
  8     int temp;
  9     printf("Bubble sorting process:\n");
 10     for (int i = 0; i < len - 1; ++i)  //Number of Cyclic Comparisons
 11     {
 12         //for (int j = 0; j < len - 1; ++j)  //Compare rounds from beginning to end
 13         for (int j = 0; j < len - 1-i; ++j)  //Comparing code optimization from beginning to end requires less than one in each round
 14         {
 15             if (arr[j]>arr[j + 1])  //Discovering two misplaced elements
 16             {
 17                 //Exchange the positions of two elements
 18                 temp = arr[j];
 19                 arr[j] = arr[j + 1];
 20                 arr[j + 1] = temp;
 21             }
 22         }
 23         //Test code
 24         for (int j = 0; j < len; ++j)
 25         {
 26             printf("%d\t", arr[j]);
 27         }
 28         printf("\n\n");
 29     }
 30 }
 31 
 32 //Selection sort
 33 void select_sort(int arr[], int len)
 34 {
 35     int k,temp;
 36     printf("Select the sorting process:\n");
 37     for (int i = 0; i < len-1; ++i)  //First out arr[i]Elements of this position
 38     {
 39         k = i;  //Save the subscript of this location as the initial condition
 40         for (int j = i + 1; j<len;++j)
 41         {
 42             if (arr[k]>arr[j])
 43             {
 44                 k = j;  //arr[j]Smaller use k Save position
 45             }
 46         }
 47         //After finding out arr[i]  arr[k]Minimum Exchange
 48         temp = arr[i];
 49         arr[i] = arr[k];
 50         arr[k] = temp;
 51         //Test code
 52         for (int j = 0; j < len; ++j)
 53         {
 54             printf("%d\t", arr[j]);
 55         }
 56         printf("\n\n");
 57     }
 58 }
 59 
 60 //Insertion sort
 61 void insert_sort(int arr[],int len)
 62 {
 63     int temp;
 64     printf("Insert sorting procedure:\n");
 65     for (int i = 1; i < len; ++i)  //Insert from the second person
 66     {
 67         //Find the right place first
 68         for (int j = 0; j < i; ++j)  //j<i It's because of comparison. i Previous elements
 69         {
 70             if (arr[j]>arr[i])  //Finding ratio arr[i]The first element to be large
 71             {    //insert
 72                 //take arr[i] Insert into arr[j]Location
 73                 temp = arr[i];  //Keep the number to insert
 74                 for (int k = i - 1; k >= j; --k)
 75                 {
 76                     arr[k + 1] = arr[k];//Move backward
 77                 }
 78                 arr[j] = temp;//Insertion element
 79                 break;  //Out of the loop, no more comparisons
 80             }
 81         }
 82         //Insert completion for the next round of loops
 83         //Test code
 84         for (int j = 0; j < len; ++j)
 85         {
 86         printf("%d\t", arr[j]);
 87         }
 88         printf("\n\n");
 89     }
 90 }
 91 
 92 //Quick sort
 93 int part(int arr[], int begin, int end)//Divide an array into two parts and have one arr[k]  than arr[k]All the small elements are in arr[k]Left ratio arr[k]All the big elements are in arr[k]Right
 94 //Return to this k Value
 95 {
 96     //1.Selection arr[begin]Make figures
 97     int i = begin, j = end, temp;
 98     while (i < j)
 99     {
100         //Find a ratio from the right arrr[begin]Small elements
101         while (i<j&&arr[j]>arr[begin]) --j;//Find a ratio. arr[begin]Small elements 
102         //Find a ratio from the left arrr[begin]Big elements
103         while (i<j&&arr[i] <= arr[begin]) ++i;
104         temp = arr[i]; 
105         arr[i] = arr[j]; 
106         arr[j] = temp;//Exchange elements
107     }
108     //When you quit  i==j And arr[i]That's where we're looking. k
109     //arr[begin] and arr[i]exchange
110     temp = arr[begin]; 
111     arr[begin] = arr[i]; 
112     arr[i] = temp;
113     return i;
114 }
115 void quick_sort(int arr[], int begin, int end)
116 {
117     if (begin >= end) return;
118     //begin Must be less than end Sorting is required
119 
120     //1.Divide into two parts
121     int k = part(arr, begin, end);//Divide into two parts
122 
123     //arr[k]The elements on the left are all smaller than arr[k]   arr[k]All elements on the right are larger than arr[k]
124     //2.Sort left
125     quick_sort(arr, begin, k - 1);//k Location does not participate in sorting, so it is k-1
126     //3.Sorting right
127     quick_sort(arr, k + 1, end);
128 }
129 
130 int main()
131 {
132 //Define disordered arrays and output them as they are
133     int arr[10] = {1,5,2,9,0,6,3,8,7,4};    //Random array
134     printf("Output before sorting:");
135     for (int i = 0; i < 10; ++i)  //Print out the elements in the array
136     {
137         printf("%d\t", arr[i]);
138     }
139     printf("\n\n");
140 
141 //Bubble sort 
142 // bullet_sort(arr, 10);  //The call function parameter values are the array name and the array size
143 
144 //Selection sort
145 //    select_sort(arr, 10);
146 
147 //Quick sort
148     quick_sort(arr, 0, 9);
149 
150     //int temp;
151     //for (int i = 0; i < 9; ++i)  //Sort the nine round
152     //{
153     //    for (int j = 0; j < 9; ++j)  //Comparisons from beginning to end
154     //    {
155     //        if (arr[j] > arr[j + 1])  //compare >From childhood to adulthood <From big to small  j+1<10 -----> j<9
156     //        {
157     //            //Exchange the positions of two elements
158     //            temp = arr[j];
159     //            arr[j] = arr[j + 1];
160     //            arr[j + 1] = temp;
161     //        }
162     //    }
163     //}
164     
165     //Output sort
166     printf("Output after sorting:");
167     for (int i = 0; i < 10; ++i)
168     {
169         printf("%d\t", arr[i]);
170     }
171     getchar();
172     return 0;
173 }

2019-04-02  17:35:13

Posted by pmjm1 on Tue, 02 Apr 2019 13:03:30 -0700