Sorting Algorithm exercise-code Summary

Source: Internet
Author: User

Insert sorting, Merge Sorting, heap sorting, fast sorting, counting sorting algorithm, C ++ source code, and so on. Pai_^

 

// Sorting Algorithm summary exercise # include "stdafx. h "# include <iostream> using namespace std; void InsertSort (); // declare the insertion sorting function void MergeSort (int num [], int left, int right ); // declare the Merge sort function void Merge (int num [], int left, int right); // declare the void HeapSort () used in the Merge sort (); // declare the heap sorting function void BuildHeap (); // declare the heap building function void HeapIfy (int I) used in heap sorting ); // declare the void QuickSort (int start, int end) function used to ensure the heap nature when the heap is created; // declare the quick sorting function int Partition (int start, int end ); // declare the segmentation function v used in quick sorting Oid CountingSort (); // declaration count sorting function int num [5]; int main () {for (int I = 0; I <= 4; I ++) cin> num [I]; int length = sizeof (num)/sizeof (num [0]); CountingSort (); // QuickSort (0, length-1 ); // HeapSort (); // InsertSort (); // MergeSort (num, 0, 4); for (int I = 0; I <5; I ++) cout <num [I] <endl; system ("PAUSE"); return 0 ;}// Insert the sorting function void InsertSort () {int n = sizeof (num) /sizeof (num [0]); // obtain the number of elements in the array to be sorted, int j, key; for (int I = 1; I <n; I ++) {j = I-1; key = num [I]; whil E (j> = 0 & num [j]> key) {num [j + 1] = num [j]; j --;} num [j + 1] = key ;}// merge the sorting functions void MergeSort (int num [], int left, int right) {if (left <right) {int I = (left + right)/2; MergeSort (num, left, I); MergeSort (num, I + 1, right); Merge (num, left, right) ;}} void Merge (int num [], int left, int right) {int I = left, j = (left + right)/2; int temp [10]; for (int q = 0; q <10; q ++) temp [q] = 0; int m = left, n = j + 1; while (m <= j & n <= right) {if (num [m] <num [n]) temp [I ++] = num [M ++]; else if (num [m]> = num [n]) temp [I ++] = num [n ++];} if (m> j) {while (n <= right) temp [I ++] = num [n ++];} else {while (m <= j) temp [I ++] = num [m ++] ;}for (I = left; I <= right; I ++) {num [I] = temp [I] ;}// heap sorting function. Note that the tree node sorting starts from 1 and the array subscript starts from 0 in heap sorting, therefore, 1 void HeapIfy (int I, int length) {int l = 2 * I; int r = 2 * I + 1; int largest, temp; if (num [L-1]> num [I-1] & l <= length) largest = l; elselargest = I; if (num [r-1]> num [largest-1] & r <= length) largest = r; if (largest! = I) {temp = num [I-1]; num [I-1] = num [largest-1]; num [largest-1] = temp; HeapIfy (largest, length) ;}} void BuildHeap () {int length = sizeof (num)/sizeof (num [0]); for (int I = length/2; I> = 1; I --) HeapIfy (I, length);} void HeapSort () {int length = sizeof (num)/sizeof (num [0]); int temp; BuildHeap (); for (int I = length; I> = 1; I --) {temp = num [I-1]; num [I-1] = num [0]; num [0] = temp; heapIfy (1, I-1) ;}// function of the Quick Sort Algorithm int Partition (int start, int end) {int x = n Um [start]; int I = start; int j = end; int temp; while (num [I] <x) {I ++ ;} while (num [j]> x) {j --;} if (I <j) {temp = num [j]; num [j] = num [I]; num [I] = temp;} else {return j;} void QuickSort (int start, int end) {if (start <end) {int m = Partition (start, end); QuickSort (start, m); QuickSort (m + 1, end) ;}// count sorting, applicable to the sorting element with a size of 1 ~ Void CountingSort () {int n = sizeof (num)/sizeof (num [0]); int numTemp [5]; // temporary storage sorting result int temp [6]; // The number of array elements is the maximum value of the sorting element + 1for (int I = 0; I <6; I ++) {temp [I] = 0 ;}for (int j = 0; j <n; j ++) {temp [num [j] = temp [num [j] + 1 ;}for (int k = 1; k <6; k ++) {temp [k] = temp [k] + temp [k-1];} for (int l = n-1; l> = 0; l --) {numTemp [temp [num [l]-1] = num [l]; temp [num [l] --;} for (int m = 0; m <n; m ++) num [m] = numTemp [m];}

 

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.