Data structure--algorithm (043) (c + + various sorting algorithm implementation)

Source: Internet
Author: User

"Disclaimer: This article is limited to self-summary and mutual exchange, there are flaws also hope you point out." Contact e-mail: [Email protected] "

Topic:

Implementation of various sorting algorithms for C + +
Topic Analysis:

Specific sorting principle reference related algorithms books

Algorithm implementation:

#include <stdio.h>static void swap (int *a, int *b) {int tmp;tmp = *a;*a = *b;*b = tmp;} static void display (int *array, int size) {int i=0; for (; i<size; ++i) printf ("%02d", Array[i]);p rintf ("\ n");} void Bubble_sort (int *array, int size) {int i,j;for (i=0; i<size-1; ++i) {for (j=i+1; j<size; ++j) {if (Array[i] > Arr AY[J]) swap (&array[i], &array[j]);}} void Select_sort (int *array, int size) {int I, J, Min;for (i=0; i<size-1; ++i) {min = I;for (j=i+1; j<size; ++j) {if (ARRA Y[i] > Array[j]) {min = j;break;}} if (min! = i) swap (&array[i], &array[j]);}}  void Quick_sort (int *array, int begin, int end) {int left = Begin;int right = End;int split = Array[begin];while (left <= right) {when (Array[left] < split && left < end) Left++;while (Array[right] > split && right > B Egin) right--;if (left <= right) swap (&array[left++], &array[right--]);} if (left < end) Quick_sort (array, left, end), if (Right > Begin) Quick_sort (array, begin, right);} VOID insert_sort (int *array, int size) {int I, j, tmp;const int width = 1;for (i=width; i<size; ++i) {tmp = ARRAY[I];J = i Width;while (J >= 0 && tmp < ARRAY[J]) {Array[j+width] = array[j];j-= width;} Array[j+width] = tmp;}} void Shell_sort (int *array, int size) {int I, j, tmp;int width = size/2;while (width >= 1) {for (i=width; i<size; ++i) {T MP = Array[i];j = I-width;while (j>=0 && tmp < ARRAY[J]) {Array[j+width] = array[j];j-= width;} Array[j+width] = tmp;} Width/= 2;}} void heap_update (int *array, int current, int top) {int i = current;int j = 2*i;int tmp = Array[current];while (J <= top) {if (J < top && Array[j] < array[j+1]) j++;if (Array[j] <= tmp) break; {Array[i] = Array[j];i = J;j = 2*i;}} Array[i] = tmp;} void Heap_sort (int *array, int size) {int i;for (I=SIZE/2; i>0;--i) heap_update (array, I, size); for (i=size; i>=2; i-- {Swap (&array[1], &array[i]); heap_update (array, 1, i-1);}} int main (int argc, char *argv[]) {int a[] = {5, 12, 67, 9, (+), 101, 3, 2, 7, 8};int size = sizeof (a)/sizeof (int);p rintf ("%-8s", "Before:");d ISPL Ay (A, size), switch (atoi (argv[1)) {case 1:printf ("Bubble_sort:"), Bubble_sort (A, size);d isplay (A, size); Break;case 2: printf ("Select_sort:"); Select_sort (A, size);d isplay (A, size), Break;case 3:printf ("Quick_sort:"); Quick_sort (A, 0, size-1);d Isplay (A, size), Break;case 4:printf ("Insert_sort:"), Insert_sort (A, size);d isplay (A, size); Break;case 5: printf ("Shell_sort:"); Shell_sort (A, size);d isplay (A, size), Break;case 6:printf ("Heap_sort:"); Heap_sort (A, size-1); Display (A, size); break;default:break;} return 0;}


Data structure--algorithm (043) (c + + various sorting algorithm implementation)

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.