Sorting Algorithm Summary

Source: Internet
Author: User
1. Fast sorting

For more information, see the quick sorting algorithm in the previous blog.

2. Heap sorting

For details, see the heap sorting implementation of non-recursive methods in the previous blog post.

3. Simple sorting (Bubble sorting, selection sorting, and insertion sorting)

The Code is as follows:

# Include <stdio. h> # include <stdlib. h> # include <time. h> # define N 20 static void show (int * arr, int Len) {int index; For (Index = 0; index <Len; index ++) {printf ("% d", arr [Index]);} printf ("\ n");} static void swap (int * left, int * right) {int TMP = * left; * Left = * right; * Right = TMP;}/*** select the sorting method: in order to put the minimum number of unordered array in the position of the POs, POS from 0 to N-1 * --> the number of front N-1 position, the last number is ignored */static void select_sort (int * arr, int Len) {Int POs, index; int min_index; For (Pos = 0; POS <len-1; POS ++) {min_index = Pos; For (Index = POS + 1; index <Len; index ++) {If (ARR [Index] <arr [min_index]) min_index = index;} If (min_index! = POS) {swap (ARR + POs, arr + min_index) ;}} static void insert_sort (int * arr, int Len) {int POs, index; int key; for (Pos = 1; POS <Len; POS ++) {key = arr [POS]; for (Index = pos-1; index> = 0; index --) {If (Key <arr [Index]) {arr [index + 1] = arr [Index];} else {break ;}} arr [index + 1] = Key ;}} static void bubble_sort (int * arr, int Len) {int POs, index; For (Pos = 0; POS <len-1; pos ++) {for (Index = 0; index <len-1-Pos; index ++) {If (ARR [Index]> arr [index + 1]) {swap (ARR + index, arr + index + 1) ;}}} static void bubble_sort2 (int * arr, int Len) {int left, right, index; left = 0; Right = len-1; while (left <right) {for (Index = left; index <right; index ++) {If (ARR [Index]> arr [index + 1]) {swap (ARR + index, arr + index + 1) ;}} right --; if (Left = right) {break;} For (Index = right; index> left; index --) {If (ARR [Index] <arr [index-1]) {swap (ARR + index, arr + index-1) ;}}left ++ ;}int main (INT argc, char * argv []) {int arr [N], index; srand (Time (null); For (Index = 0; index <n; index ++) {arr [Index] = rand () % 100 + 1;} Show (ARR, n); bubble_sort2 (ARR, n); show (ARR, n); System ("pause"); Return 0 ;}
4. Merge Sorting

To be continued.

Sorting Algorithm Summary

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.