// Insertion-Sort.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
using namespace std;
#define LEN 10
double * Insertion_Ascending_Sort(double *Tmp, int Len)//插入升序函数;
{
double key;
for (int i = 1 , j = 0; i < Len; i++)
{
key = Tmp[i];
j = i - 1;
while ((j >= 0) && (Tmp[j]) > key)
{
Tmp[j + 1] = Tmp[j];
j--;
}
Tmp[j + 1] = key;
}
return Tmp;
}
double * Insertion_Descending_Sort(double *Tmp, int Len)//插入降序函数;
{
double key;
for (int i = 1, j = 0; i < Len; i++)
{
key = Tmp[i];
j = i - 1;
while ((j >= 0) && (Tmp[j] < key))
{
Tmp[j + 1] = Tmp[j];
j--;
}
Tmp[j + 1] = key;
}
return Tmp;
}
int _tmain(int argc, _TCHAR* argv[])
{
double *a = new double[LEN];
double *b = new double[LEN];
srand((unsigned)time(NULL));//产生随机数种子;
for (int i = 0; i < LEN; i++)//为数组赋随机数初值;
{
b[i] = a[i] = rand();
}
a = Insertion_Descending_Sort(a, LEN);//插入降序函数;
b = Insertion_Ascending_Sort(b, LEN);//插入升序函数
for (int i = 0; i < LEN - 1; i++)
{
cout<< setw(6) << a[i] << ',';
}
cout<< setw(6) << a[LEN - 1] <<endl;
for (int i = 0; i < LEN - 1; i++)
{
cout<< setw(6) << b[i] << ',';
}
cout<< setw(6) << b[LEN - 1] <<endl;
getchar();
delete[] a;
delete[] b;
return 0;
}
//
#include "stdafx.h"
using namespace std;
#define LEN 10
double * Insertion_Ascending_Sort(double *Tmp, int Len)//插入升序函数;
{
double key;
for (int i = 1 , j = 0; i < Len; i++)
{
key = Tmp[i];
j = i - 1;
while ((j >= 0) && (Tmp[j]) > key)
{
Tmp[j + 1] = Tmp[j];
j--;
}
Tmp[j + 1] = key;
}
return Tmp;
}
double * Insertion_Descending_Sort(double *Tmp, int Len)//插入降序函数;
{
double key;
for (int i = 1, j = 0; i < Len; i++)
{
key = Tmp[i];
j = i - 1;
while ((j >= 0) && (Tmp[j] < key))
{
Tmp[j + 1] = Tmp[j];
j--;
}
Tmp[j + 1] = key;
}
return Tmp;
}
int _tmain(int argc, _TCHAR* argv[])
{
double *a = new double[LEN];
double *b = new double[LEN];
srand((unsigned)time(NULL));//产生随机数种子;
for (int i = 0; i < LEN; i++)//为数组赋随机数初值;
{
b[i] = a[i] = rand();
}
a = Insertion_Descending_Sort(a, LEN);//插入降序函数;
b = Insertion_Ascending_Sort(b, LEN);//插入升序函数
for (int i = 0; i < LEN - 1; i++)
{
cout<< setw(6) << a[i] << ',';
}
cout<< setw(6) << a[LEN - 1] <<endl;
for (int i = 0; i < LEN - 1; i++)
{
cout<< setw(6) << b[i] << ',';
}
cout<< setw(6) << b[LEN - 1] <<endl;
getchar();
delete[] a;
delete[] b;
return 0;
}