Look at the data structure write code (62) Insert Sort

Source: Internet
Author: User

InsertSort.cpp: Defines the entry point of the console application. Interpolation sort # include "stdafx.h" #include <cstdlib>static int testarray[] = {0,55,33,22,99,77,66,11,44,88,9};//11// Array number No. 0 position as Sentinel ...//ascending sort void insertsort (int * Array,int num) {for (int i = 2; I <= num; i++) {if (Array[i] < array[i-1]) { Array[0] = array[i];//copy Sentinel. array[i] = array[i-1];int j = i-2;for (; Array[j] > Array[0]; j--) {//move back array[j+1] = array[ j];} ARRAY[J+1] = array[0];}} Binary insert sort void halfinsertsort (int *array,int num) {for (int i = 2; I <= num; i++) {array[0] = array[i];//copy Sentinel int low = 1;in T high = i-1;while (Low <= high) {int mid = (low + high)/2;if (Array[mid] > Array[0]) {high = mid-1;} Else{low = mid +1;}} Int j = i-1;for (; j >= high+1; j--) {array[j+1] = array[j];} ARRAY[J+1] = array[0];}} 2-Way insertion sort void p2_insertsort (int * array,int num) {int size = sizeof (int) * (num); int * p = (int *) malloc (size); int first = 0 ; int final = 0;p[0] = array[1];for (int i = 2; I <= num; i++) {if (Array[i] < P[first]) {first = (first-1 + num)%Num;p[first] = Array[i];} else if (Array[i] > P[final]) {final = (final + 1)% num;p[final] = array[i];} Else{int j = final++;for (; P[j] > Array[i];) {int index = (j+1)%num;p[index] = P[j];j = (j-1 + num)% num;} p[(j+1)%num] = Array[i];}} for (int i = 0; i < num; i++) {int index = (first +i)% num;array[i+1] = P[index];} Free (p);} void Randomarray () {for (int i = 1; i <=; i++) {Testarray[i] = rand ()% 100;}} void PrintArray () {for (int i = 1; i <; i++) {printf ("%d\t", Testarray[i]);} printf ("\ n");} Static table Insert Sort,//do not need to move, only need to change next "pointer" #define SIZE 100struct slnode{int data;int next;}; struct sltable{//static linked list Slnode Base[size];int len;};/ /initialize static linked list void Inittable (sltable * t,int * array,int num) {for (int i = 1; I <= num; i++) {t->base[i].data = Array[i];} T->base[1].next = 0;t->base[0].next = 1;t->base[0].data = Int_max;t->len = num;} void Tableinsertsort (sltable * t,int * array,int num) {for (int i = 2; I <= num; i++) {int pre = 0;int next = t->base[ 0].next;while (Next! = 0) {Slnode node = t->base[next];if (Node.data > Array[i]) {break;} Pre = Next;next = T->base[next].next;} T->base[i].next = T->base[pre].next;t->base[pre].next = i;}} void printtable (Sltable t) {int next = T.base[0].next;while (next! = 0) {printf ("%d\t", t.base[next].data); next = t.base[ Next].next;}} Shell sort//reduce delta sort void Shellinsert (int * array,int Num,int DT) {for (int i = dt + 1; I <= num; i++) {if (Array[i] < AR RAY[I-DT]) {array[0] = array[i];int j = i-dt;for (; j > 0 && array[j] > array[0]; j-=dt) {Array[j+dt] = Arra Y[J];} ARRAY[J+DT] = array[0];}} static int dtarray[] = {5,3,2,1};void shellsort (int * Array,int num) {for (int i = 0; i < 4; i++) {Shellinsert (Array,num, Dtarray[i]);}} int _tmain (int argc, _tchar* argv[]) {//srand (5000);//Insert sort printf ("----------------Insert sort---------------\ n"); Randomarray (); Insertsort (testarray,10);p rintarray ();//dichotomy Insert sort printf ("----------------binary insertion sort---------------\ n"); Randomarray (); Halfinsertsort (testarray,10);p rintarray ();//two-way pluginto sort printf ("----------------two-way insertion sort---------------\ n"); Randomarray ();p 2_insertsort (testarray,10);p Rintarray (); printf ("----------------static list insert sort---------------\ n"); Randomarray (); Sltable t;inittable (&t,testarray,10); Tableinsertsort (&t,testarray,10);p rinttable (t);//shell sortprintf (" ----------------Hill sort---------------\ n "); Randomarray (); Shellsort (testarray,10);p Rintarray (); return 0;}



Look at the data structure write code (62) Insert Sort

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.