# Various sorting algorithm exercises

Source: Internet
Author: User

Recently busy ready to find a job, put the sorting algorithm before writing, write together, convenient later use, nonsense not to say, directly on the code;

`#include <iostream> #include <time.h>using namespace std; #define N 100//#define SWAP (x, y) {int t;t=x;x=y;y=t ;} #define SWAP (x, y) {x=x^y;y=x^y;x=x^y;}    Radix sort void Radixsort (int data[], int n) {//auxiliary function, for the maximum number of digits of the data int d = 1;//Save the largest number of digits int p = 10;            for (int i = 0, i < n; ++i) {while (Data[i] >= p) {p *= 10;        ++d;    }}//Radix sort int *tmp = new Int[n]; int *count = new INT;    counter int I, j, K;    int radix = 1; for (i = 1; I <= D; i++)//D-Order {for (j = 0; J < K J + +) Count[j] = 0;//empty counter before each allocation fo        R (j = 0; J < N; j + +) {k = (Data[j]/radix)% 10;//Count the number of records in each bucket count[k]++; } for (j = 1; J <, J + +) Count[j] = Count[j-1] + count[j]; Assign the position in TMP to each bucket for (j = n-1; J >= 0; j--)//Collect all the records in the bucket sequentially into tmp {k = (Data[j]/radix)% 10            ;            TMP[COUNT[K]-1] = data[j];        count[k]--;      }  for (j = 0; J < N; j + +)//Copy the contents of the temporary array to data data[j] = Tmp[j];    Radix = radix * 10;    } delete[]tmp; Delete[]count;} Shell sort, improved insert sort int shellsort (int *number) {for (int group=n/2;group>0;group/=2) {for (int i=group;i<n;i++)// Each one is looped, so the following swaps only once {for (int j=i-group;j>=0&& (Number[j]>number[j+group]), J-=group)//can only be exchanged once at a time, It's not right to swap too much. {swap (Number[j],number[j+group]);}}} return 0;} Insert sort int insort (int *number) {/*int temp;for (int i=n-2;i>=0;i--)//Sort from large to small loop {temp=number[i];int J=i+1;while (temp >number[j]) {number[j-1]=number[j];j++;if (j==n) break;} Number[j-1]=temp;} */int temp,j;for (int i=1;i<n;i++)//Sort from small to large loop {temp=number[i];for (j=i;j>0;j--) {if (number[j-1]>temp) { NUMBER[J]=NUMBER[J-1];} Else{break;}} Number[j]=temp;} return 0;} Heap algorithm, heap sort, improved selection sort int heapsort (int *number) {//build sort heap Tree int heap[n+1]={-1111},child,parent;for (int i=1;i<=n;i++) {Heap[i]=number[i-1];child=i;parent=i/2;while (heap[child]　　Various sorting algorithm exercises`
Related Keywords:

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.