Various sorting algorithms (serialization ...), The sorting algorithm is being serialized...

Source: Internet
Author: User

Various sorting algorithms (serialization ...), The sorting algorithm is being serialized...

To open a new blog, start with the sorting algorithm... I try my best to start with the simplest Sorting Algorithm and occasionally upload updates ~

Ps: Before I learned JAVA and C ++, I used C to write the program. The level is limited. Let's take a look at it and focus on algorithms !~

(Image Source: Network)

Directory:

1. Insert sort-insert sort directly.

2. Insert sort-hill sort.

3. Select sort-simply select sort.

4. Select sorting-heap sorting.

5. Exchange sorting-Bubble sorting.

6. Exchange sorting-fast sorting (recommended ).

7. Merge and sort.

8. Base sorting.

 

1. Insert sort-insert sort directly.

The specific algorithm is described as follows: (I think it is easy to understand)

Ps: the binary search method can be used to reduce the number of comparison operations.

The C language code is as follows:

1 # include <stdio. h> 2 # define MAXN 10000 3 int main () 4 {5 printf ("enter a set of numbers separated by commas, enter key end input \ n "); 6 int arr [MAXN]; // defines an array large enough 7 int I = 0, j, k, count = 0; 8 while (count <MAXN) // read the array and store it in array a; 9 {10 scanf ("% d", & arr [I ++]); 11 count ++; 12 if (getchar () = '\ n') break; 13} 14 int target; 15 for (I = 1; I <count; I ++) // start sorting 16 {17 j = I; 18 target = arr [I]; // starting from the second number, reference point 19 while (j> 0 & target <arr [J-1]) 20 {21 arr [j] = arr [J-1]; 22 j --; 23} 24 arr [j] = target; 25} 26 for (I = 0; I <count; I ++) 27 printf ("% d ", arr [I]); 28 return 0; 29}<-- Click + on the left to expand

Ps: Modify: input is separated by a space

2. Insert sorting-hill sorting (the efficiency is further improved compared with direct insert sorting ).

Shell Sort is a Sort of insert. Is an improvement for directly inserting sorting algorithms. This method, also known as downgrading incremental sorting, was named after DL. Shell was proposed in 1959. Hill sort (incremental sort) Thought: In 1959, Donald L. Shell proposed Shell sort, derived from the improvement of direct Insertion Algorithm.

The specific algorithm is described as follows:

First, the entire sequence of elements to be arranged is divided into several sub-sequences (composed of elements separated by an increment) for direct insertion and sorting, and then the incremental sequence is reduced and then sorted, when the elements in the entire sequence are basically ordered (incremental as 1), all the elements are directly inserted and sorted. Because the efficiency of direct insert sorting is very high when the elements are basically ordered (close to the best condition), the time efficiency of hill sorting is much higher than that of the first two methods.

The C language code is as follows:

1 # include <stdio. h> 2 # define MAXN 10000 3 4 void shellsort (int a [], int n) 5 {6 int I, j, gap; 7 for (gap = n/2; gap> 0; gap/= 2) 8 {9 for (I = 0; I <gap; I ++) // directly Insert the sorting 10 {11 for (j = I + gap; j <n; j + = gap) 12 if (a [j] <a [j-gap]) 13 {14 int temp = a [j]; 15 int k = j-gap; 16 while (k> = 0 & a [k]> temp) 17 {18 a [k + gap] = a [k]; 19 k-= gap; 20} 21 a [k + gap] = temp; 22} 23} 24} 25} 26 27 int main () 28 {29 printf ("enter a group of numbers separated by one space, enter key end input \ n "); 30 int arr [MAXN]; // defines an array of 31 int I = 0, j, k, count = 0; 32 while (count <MAXN) // read the array and store it in array a; 33 {34 scanf ("% d", & arr [I ++]); 35 count ++; 36 if (getchar () = '\ n') break; 37} 38 shellsort (arr, count); 39 for (I = 0; I <count; I ++) 40 printf ("% d", arr [I]); 41 return 0; 42}<-- Click + on the left to expand

3. Select sort-simply select sort.

A simple and easy-to-understand algorithm 0.0

The principle is as follows (from Baidu encyclopedia ):

Set the number of records in the sorted sequence to n. I: 1, 2 ,..., N-1, from all n-I + 1 records (Ri, Ri + 1 ,..., Rn) to find the record with the smallest sorting code and exchange it with the I record. The record sequence is sorted after the n-1 round is executed.

The C language code is as follows:

1 # include <stdio. h> 2 # define MAXN 10000 3 int main () 4 {5 printf ("enter a set of numbers separated by commas, enter key end input \ n "); 6 int arr [MAXN]; // defines an array large enough 7 int I = 0, j, k, count = 0; 8 while (count <MAXN) // read the array and store it in array a; 9 {10 scanf ("% d", & arr [I ++]); 11 count ++; 12 if (getchar () = '\ n') break; 13} 14 15 for (I = 0; I <count; I ++) 16 {17 k = I; 18 for (j = I + 1; j <count; j ++) 19 {20 if (a [k]> a [j]) 21 k = j; 22} 23 if (k! = I) 24 {25 t = a [I]; // swap 26 a [I] = a [k]; 27 a [k] = t; 28} 29 30} 31 for (I = 0; I <count; I ++) 32 printf ("% d", arr [I]); 33 return 0; 34}<-- Click + on the left to expand

4. Select sorting-heap sorting.

 To be supplemented...

5. Exchange sorting-Bubble sorting.

The name of this algorithm comes from because the larger element will slowly "float" to the top of the series through the exchange. Due to the concise nature of Bubble sorting, it is usually used to introduce the concept of algorithms to students who are getting started with computer programming. The operation of the Bubble Sorting Algorithm is as follows: (from Baidu encyclopedia)
  1. Compares adjacent elements. If the first is bigger than the second, exchange the two of them.
  2. Perform the same operation on each adjacent element, from the first to the last. At this point, the final element should be the largest number.
  3. Repeat the preceding steps for all elements except the last one.
  4. Continue to repeat the above steps for fewer and fewer elements until there is no need to compare them.

The C language code is as follows:

1 # include <stdio. h> 2 # define MAXN 10000 3 4 void bubble_sort (int a [], int n) 5 {6 int I, j, temp; 7 for (j = 0; j <n-1; j ++) 8 {9 for (I = 0; I <n-1-j; I ++) 10 {11 if (a [I]> a [I + 1]) // sort the array element size in ascending order 12 {13 temp = a [I]; // exchange 14 a [I] = a [I + 1]; 15 a [I + 1] = temp; 16} 17} 18} 19} 20 int main () 21 {22 printf ("Enter a set of numbers separated by one space and Enter \ n"); 23 int arr [MAXN]; // define a sufficiently large array 24 int I = 0, j, k, count = 0; 25 while (count <MAXN) // read the array and store it in array; 26 {27 scanf ("% d", & arr [I ++]); 28 count ++; 29 if (getchar () = '\ n') break; 30} 31 bubble_sort (arr, count); 32 for (I = 0; I <count; I ++) 33 printf ("% d", arr [I]); 34 return 0; 35}<-- Click + on the left to expand

6. Exchange sorting-fast sorting (recommended ).

I finally wrote my favorite quick sorting algorithm.

The principles are as follows: (from Baidu encyclopedia)

Quicksort is an improvement in Bubble sorting. Proposed by C. A. R. Hoare in 1962. Its basic idea is: Split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence. 1 # include <stdio. h> 2 # define MAXN 10000 3 4 void QuickSort (int a [], int numsize) 5 {6 int I = 0, j = numsize-1; 7 int key = a [0]; 8 if (numsize> 1) // make sure that the array length is at least 2; otherwise, sort 9 {10 while (I <j) // loop end condition 11 {12 for (; j> I; j --) 13 if (a [j] <key) 14 {15 a [I ++] = a [j]; 16 break; 17} 18 for (; I <j; I ++) 19 if (a [I]> key) 20 {21 a [j --] = a [I]; 22 break; 23} 24} 25 a [I] = key; 26 QuickSort (a, I); // recursion 27 QuickSort (a + I + 1, numsize-i-1); // recursion 28} 29} 30 31 int main () 32 {33 printf ("Enter a set of numbers separated by one space and Enter \ n"); 34 int arr [MAXN]; // define an array 35 int I = 0, j, k, count = 0; 36 while (count <MAXN) // read the array and store it in array; 37 {38 scanf ("% d", & arr [I ++]); 39 count ++; 40 if (getchar () = '\ n') break; 41} 42 QuickSort (arr, count); 43 for (I = 0; I <count; I ++) 44 printf ("% d", arr [I]); 45 return 0; 46}<-- Click + on the left to expand

7. Merge and sort.

 To be supplemented...

8. Base sorting.

To be supplemented...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.