insertion sort

来源:互联网 发布:qq飞车s车麦凯伦数据 编辑:程序博客网 时间:2024/05/16 17:01
// insertion sort.cpp : Defines the entry point for the console application.
/***********************************************************************************
* 1 yield random number: srand(unsigned (time(0))), rand()
* 2 insert sort
* 3 insert_sort(int s[],)参数int s[] 等价于 int * const s;
* 4 non-decreasing insert_sort(,)
* 5 selection sort()
* date: Sep./16/2012
*
* 6 insertion_sort_recursive() express insertion sort as a recursive procedure
* 7 don't srand in subroutine, which leads to the same random results
* date: Sep./19/2012
*
* Author: Mr. Rocky Chen
************************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <ctime> //time()
#include <cstdlib>  //srand(),rand()
#include <string>


using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
void insert_sort(int * const, int);
void insert_sort_non_decreasing(int * const, int);
void selection_sort(int *const s, int length);
void insertion_sort_recursive(int * const a, int length);
void random_array(int * const a, int length); //yield ramdon array
void print_array(int * const a, int length, string str); //print array elements


const int length = 10;
int s[length];
srand(unsigned (time(0))); //don't srand in subroutine, which leads to the same random results


//insert sort****
random_array(s,length);
insert_sort(s,length);
print_array(s,length,"insertion sort");


//insertion sort recursive version
random_array(s,length);
insertion_sort_recursive(s,length);
print_array(s,length,"insertion sort recursive");


//insert sort non-decreasing****
random_array(s,length);
insert_sort_non_decreasing(s,length);
print_array(s,length,"insertion sort non-decreasing");


//selection sort*****
random_array(s,length);
selection_sort(s,length);
print_array(s,length,"selection sort");


system("pause");
return 0;
}


void random_array(int * const a, int length)
{
cout<<"random array ===========" << endl;
for (int i = 0; i < length; i++)
{
a[i] = rand()%30;
cout << "a[" << i <<"] = " << a[i] << endl;
}
}


void print_array(int * const a, int length, string str)
{
cout << str << "==========" << endl;
for (int i = 0; i < length; i++)
{
cout << "a[" << i <<"] = " << a[i] << endl;
}
}


void insert_sort(int * const s, int length)
{


//从第二个元素开始排序,和之前的元素不断比较,交换,插入。
for(int j = 1; j < length; j++)
{
int key = s[j];
int i;
for(i = j-1; i >= 0 && s[i] > key; s[i+1] = s[i--]); //i--
s[i+1] = key;
}
}


//non-decreasing insert sort. 
void insert_sort_non_decreasing(int * const s, int length)
{
for( int j = 1; j < length; j++)
{
int key  = s[j];
int i;
for(i = j-1; i >= 0 && s[i] < key; s[i+1]= s[i--]); // s[i] < key
s[i+1] = key;
}
}


void selection_sort(int *const s, int length)
{
for(int j=0; j < length-1; j++)  //loop lenght-1 times
{
int min = s[j];
int location = j;
for(int i = j; i < length; i++) //find minial number between j to length, hold mininal number and its location 
{
if( min > s[i])
{
min = s[i];
location = i;
}
}
s[location]= s[j]; //swep minimal number with s[j];
s[j]=min;
}
}


// insertion sort recursive version
void insertion_sort_recursive(int * const a, int length)
{
void insert(int * const a, int length);
if(length > 1)
{
insertion_sort_recursive(a, length-1);  //finish f(n-1)
insert(a, length); //insert Nth element
}
}


//subroution, insert a[length-1] to appropriate position. 
void insert(int * const a, int length)
{
int key = a[length-1];
int i;
for(i = length-2; i >= 0 && a[i] > key; a[i+1]= a[i--]); 
a[i+1] = key;
}