Common sorting in C Language

Source: Internet
Author: User

1. Bubble Sorting

 
# Include "stdio. H "Void bubble_sort (INT arr [], int N) {int I, j, temp; for (I = 0; I <n-1; I ++) {for (j = 0; j <n-i-1; j ++) {If (ARR [J]> arr [J + 1]) {temp = arr [J]; arr [J] = arr [J + 1]; arr [J + 1] = temp ;}} return ;}

For the above Bubble sorting, if all the data in the array has been sorted before the first loop is completed, then you need to improve the specificCodeAs follows:

 
# Include "stdio. H "Void bubble_sort (INT arr [], int N) {int I, j, flag, temp; for (I = 0; I <n-1; I ++) {flag = 1; for (j = 0; j <n-i-1; j ++) {If (ARR [J]> arr [J + 1]) {temp = arr [J]; arr [J] = arr [J + 1]; arr [J + 1] = temp; flag = 0 ;} if (1 = Flag) break; // jump out of the latest loop} return ;}

 

2. Select sorting

Compare each number with the number next to it, and exchange the smallest number with it. This ensures that the numbers before the benchmark and the benchmark are both ordered.

 
# Include "stdio. H "Void choose_sort (int A [], int N) {int I, J, K, temp; for (I = 0; I <n-1; I ++) {k = I; for (j = I + 1; j <n; j ++) {if (a [J] <A [k]) {k = J ;} if (K! = I) {temp = A [I]; A [I] = A [k]; A [k] = temp ;}}}}

 

3. Fast sorting

 

 
# Include "stdio. H" # define N 6
// Divide the benchmark int partition (INT arr [], int low, int hight) {int key; Key = arr [low]; // Save the maximum number of baselines, therefore, you can overwrite the while (low  

 

4. Merge and sort

The basic idea of merging and sorting is to divide the sequence to be sorted into two parts, and use merging and sorting for the two parts in turn, and then merge them.

# Include "stdio. H "Void merge (INT arr [], int low, int mid; int high) {int I, K; int * temp = (int *) malloc (high-low + 1) * sizeof (INT); // apply for space int left_low = low; int left_high = mid; int right_low = Mid + 1; int right_high = high; // compare the elements pointed to by the two pointers for (k = 0; left_low <= left_high & right_low <= right_high; k ++) {If (ARR [left_low] <= arr [right_low]) {temp [k] = arr [left_low ++];} else {temp [k] = arr [right_low ++] ;}}// if the first sequence has surplus, copy it directly and paste it to the end of the merged sequence if (left_low <= left_high) {memcpy (temp + k, arr + left_low, (left_high-left_low + 1) * sizeof (INT); for (I = left_low; I <= left_high; I ++) {temp [k ++] = arr [I] ;}// if the second sequence has surplus, copy it and paste it to the end of the merged sequence if (right_low <= right_high) {memcpy (temp + k, arr + right_low, (right_high-right_low + 1) * sizeof (INT); for (I = right_low; I <= right_high; I ++) {temp [k ++] = arr [I] ;}} for (I = 0; I 

 

 

 

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.