直接插入排序(Straight Insertion Sort):将一个记录插入到已经排好序的有序表中,从而得到一个新的、记录数增1的有序表。
#include<iostream>
using namespace std;
//直接插入排序
int straightInsertionSort(int* arr,int length);
void swap(int& elem1,int& elem2);
void test();
void printArr(int* arr,int length);
void swap(int& elem1,int& elem2)
{
int tmp = elem1;
elem1 = elem2;
elem2 = tmp;
}
int straightInsertionSort(int* arr,int length)
{
if(NULL==arr||length<=0)
return -1;
for(int idx=1;idx!=length;++idx)
{
//把后面的第idx个元素插入到前面的有序数组0~idx-1中
int tmp = arr[idx];
int behindIdx = idx - 1;
while(behindIdx>=0&&arr[behindIdx]>tmp)
{
arr[behindIdx+1] = arr[behindIdx];
--behindIdx;
}
arr[++behindIdx] = tmp;
}
return 0;
}
|
void printArr(int* arr,int length)
{
if(NULL==arr||length<=0)
return ;
for(int idx=0;idx!=length;++idx)
{
cout<<arr[idx]<<" ";
}
cout<<endl;
}
void test()
{
int arr[] = {6,5,3,1,8,7,2,4};
printArr(arr,8);
straightInsertionSort(arr,8);
printArr(arr,8);
cout<<endl;
int arr1[] = {1,2,3,4,5,6,7,8};
printArr(arr1,8);
straightInsertionSort(arr1,8);
printArr(arr1,8);
cout<<endl;
int arr2[] = {2,2,2,2};
printArr(arr2,4);
straightInsertionSort(arr2,4);
printArr(arr2,4);
cout<<endl;
int arr3[] = {2,2,1,2};
printArr(arr3,4);
straightInsertionSort(arr3,4);
printArr(arr3,4);
cout<<endl;
int* arr4 = NULL;
printArr(arr4,4);
straightInsertionSort(arr4,4);
printArr(arr4,4);
cout<<endl;
}
int main()
{
test();
system("pause");
}
|
//直接插入排序改进,使用二分查找找到要插入元素的位置,然后才开始移动元素。
#include<iostream>
using namespace std;
//直接插入排序改进,二分插入排序
//对左边已经排好序的序列使用二分查找,找到右边序列待插入元素要插入的位置,然后在整体移动
void test();
void printArr(int* arr,int length);
int binaryInsertionSort(int* arr,int length);
int binaryInsertionSort(int* arr,int length)
{
if(NULL==arr||length<=0)
return -1;
for(int idx=1;idx!=length;++idx)
{
int tmp = arr[idx];
int left = 0;
int right = idx-1;
int middle;
while(left<=right)
{
middle = (left+right)/2;
if(arr[middle]<=tmp) //稳定排序
left = middle + 1;
else if(arr[middle]>tmp)
right = middle - 1;
}
for(int iidx=idx-1;iidx>=left;--iidx)
{
arr[iidx+1] = arr[iidx];
}
arr[left] = tmp;
}
return 0;
}
|
void printArr(int* arr,int length)
{
if(NULL==arr||length<=0)
return ;
for(int idx=0;idx!=length;++idx)
{
cout<<arr[idx]<<" ";
}
cout<<endl;
}
void test()
{
int arr[] = {6,5,3,1,8,7,2,4};
printArr(arr,8);
binaryInsertionSort(arr,8);
printArr(arr,8);
cout<<endl;
int arr1[] = {1,2,0,-1,5,6,7,8};
printArr(arr1,8);
binaryInsertionSort(arr1,8);
printArr(arr1,8);
cout<<endl;
int arr2[] = {2,2,2,2};
printArr(arr2,4);
binaryInsertionSort(arr2,4);
printArr(arr2,4);
cout<<endl;
int arr3[] = {2,2,1,2};
printArr(arr3,4);
binaryInsertionSort(arr3,4);
printArr(arr3,4);
cout<<endl;
int arr5[] = {1,2,3,4,5,6,7,8};
printArr(arr5,8);
binaryInsertionSort(arr5,8);
printArr(arr5,8);
cout<<endl;
int* arr6 = NULL;
printArr(arr6,4);
binaryInsertionSort(arr6,4);
printArr(arr6,4);
cout<<endl;
}
int main()
{
test();
system("pause");
}
|