Various common sorting implementation Codes

Source: Internet
Author: User

It mainly includes bubble, simple selection, insertion, heap sorting, merge, and fast sorting. It will be supplemented later.

It may be wrong. Please note.

# Include <iostream> # include <cstdio> using namespace STD; // ascending int arr [10000], length; // bubble sort void bubble_sort (int * arr, int length) {bool update; while (1) {update = false; For (INT I = 0; I <length-1; ++ I) if (ARR [I]> arr [I + 1]) {int temp = arr [I]; arr [I] = arr [I + 1]; arr [I + 1] = temp; update = true;} If (! Update) Break ;}/// simple sorting void simpleeelect_sort (int * arr, int length) {for (INT I = 0; I <length; ++ I) {int Minn = I; for (Int J = I + 1; j <length; ++ J) if (ARR [J] <arr [Minn]) Minn = J; if (Minn! = I) {int temp = arr [I]; arr [I] = arr [Minn]; arr [Minn] = temp ;}}} // Insert the sorted void insert_sort (int * arr, int length) {for (INT I = 1; I <length; ++ I) {int key = arr [I]; int J; For (j = I-1; j> = 0; -- j) {If (ARR [J] <= Key) break; else arr [J + 1] = arr [J];} arr [J + 1] = key;} // heap void heapadjust (int * arr, int RT, int length) {int largest = RT; int key = arr [RT]; while (1) {int lc = RT * 2 + 1, Rc = RT * 2 + 2; if (LC <length & arr [LC]> arr [Largest]) largest = Lc; If (RC <length & arr [RC]> arr [Largest]) largest = RC; If (largest = RT) break; arr [RT] = arr [Largest]; arr [Largest] = key; RT = largest ;}} void heapbuild (int * arr, int length) {for (INT I = (length-1)/2; I> = 0; -- I) heapadjust (ARR, I, length);} void heap_sort (int * arr, int length) {heapbuild (ARR, length); For (INT I = length-1; I> = 1; -- I) {int temp = arr [I]; arr [I] = arr [0]; arr [0] = temp; heapadjust (ARR, 0, I );}} // merge and sort int A [10000]; void merge_sort (int * arr, int L, int R) {If (L <r) {int M = (L + r) /2; merge_sort (ARR, l, m); merge_sort (ARR, m + 1, R); int P = L, q = m + 1, I = L; while (P <= M | q <= r) {If (q> r | (P <= M & arr [p] <= arr [Q]) A [I ++] = arr [p ++]; else a [I ++] = arr [q ++] ;}for (Int J = L; j <= r; ++ J) Arr [J] = A [J] ;}}// fast sorting int partition (int * arr, int L, int R) {int key = arr [R]; int I = L-1; For (Int J = L; j <= r; ++ J) if (ARR [J] <= key) {I ++; int temp = arr [J]; arr [J] = arr [I]; arr [I] = temp;} return I ;} void quick_sort (int * arr, int L, int R) {If (L <r) {int mid = partition (ARR, L, R); quick_sort (ARR, l, mid-1); quick_sort (ARR, Mid + 1, R) ;}} void read () {scanf ("% d", & length ); for (INT I = 0; I <length; ++ I) scanf ("% d", & arr [I]);} void output () {for (INT I = 0; I <length; ++ I) printf ("% d", arr [I]); printf ("\ n ");} int main () {read (); quick_sort (ARR, 0, length-1); output (); Return 0 ;}

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.