常用排序算法 总结
1. 直接插入排序
2. 折半插入排序
3. 冒泡排序
4. 简单选择排序
5. 希尔排序
6. 快速排序
7. 堆排序
8. 二路归并排序
[cpp] // Sort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
#include "iostream"
using namespace std;
// Straight insert sort
void InsertSort(int *arr,int length);
void InsertSort2(int arr[], int length);
// Binary Insert Sort
void BinInsertSort(int arr[], int length);
// Shell Sort
void ShellSort(int *arr,int length);
// Shell Sort 2
void ShellSort2(int *arr,int length);
// Bubble Sort
void BubbleSort(int *arr,int length);
// Quick Sort
void QuickSort(int *arr, int low, int high);
// Partition
int Partition(int *arr, int low, int high);
// Simple Selection Sort
void SimpleSelectionSort(int *arr, int length);
// HeapAdjust
void HeapAdjust(int *arr, int s,int end);
// Heap Sort,小根堆
void HeapSort(int *arr,int length);
// Merge Array
void Merge(int *arr, int *arr2, int left, int mid, int right);
// Merge Sort
void MergeSort(int *arr, int *arr2, int left, int right);
int _tmain(int argc, _TCHAR* argv[])
{
int arr[]={29,5,44,28,36,42,7,5,88,9,10};
int length=sizeof(arr)/sizeof(*arr);
InsertSort(arr,length);
printf("Straight Insert Sort:\n");
printf("total number:%4d\n",length);
printf("sorted result:\n");
for (int i=0;i<length;i++)
{
printf("%5d",arr[i]);
}
cout<<endl;
// test2
int arr2[]={29,5,44,28,36,42,7,5,88,9,10};
length=sizeof(arr2)/sizeof(*arr2);
InsertSort2(arr2,length);
printf("Straight Insert Sort:\n");
printf("total number:%4d\n",length);
printf("sorted result:\n");
for (int i=0;i<length;i++)
{
printf("%5d",arr2[i]);
}
cout<<endl;
// test3
int arr3[]={29,5,44,28,36,42,7,5,88,9,10};
length=sizeof(arr3)/sizeof(*arr3);
BinInsertSort(arr3,length);
printf("Binary Insert Sort:\n");
printf("total number:%4d\n",length);
printf("sorted result:\n");
for (int i=0;i<length;i++)
{
printf("%5d",arr3[i]);
}
cout<<endl;
// test4
int arr4[]={29,5,44,28,36,42,7,5,88,9,10};
length=sizeof(arr4)/sizeof(*arr4);
ShellSort(arr4,length);
printf("Shell Sort:\n");
printf("total number:%4d\n",length);
printf("sorted result:\n");
for (int i=0;i<length;i++)
{
printf("%5d",arr4[i]);
}
cout<<endl;
// test5
int arr5[]={29,5,44,28,36,42,7,5,88,9,10};
length=sizeof(arr5)/sizeof(*arr5);
ShellSort2(arr5,length);
printf("Shell Sort 2:\n");
printf("total number:%4d\n",length);
printf("sorted result:\n");
for (int i=0;i<length;i++)
{
printf("%5d",arr5[i]);
}
cout<<endl;
// test6
int arr6[]={29,5,44,28,36,42,7,5,88,9,10};
length=sizeof(arr6)/sizeof(*arr6);
BubbleSort(arr6,length);
printf("Bubble Sort:\n");
printf("total number:%4d\n",length);
printf("sorted result:\n");
for (int i=0;i<length;i++)
{
printf("%5d",arr6[i]);
}
cout<<endl;
// test7
int arr7[]={29,5,44,28,36,42,7,5,88,9,10};
length=sizeof(arr7)/sizeof(*arr7);
QuickSort(arr7,0,length-1);
printf("Quick Sort:\n");
printf("total number:%4d\n",length);
printf("sorted result:\n");
for (int i=0;i<length;i++)
{
printf("%5d",arr7[i]);
}
cout<<endl;
// test8
int arr8[]={29,5,44,28,36,42,7,5,88,9,10};
length=sizeof(arr8)/sizeof(*arr8);
SimpleSelectionSort(arr8,length);
printf("Simple Selection Sort:\n");
printf("total number:%4d\n",length);
printf("sorted result:\n");
for (int i=0;i<length;i++)
{
printf("%5d",arr8[i]);
}
cout<<endl;
// test9, 对于堆的应用,数组arr[0]暂时不用,降低处理复杂度
int arr9[]={0,29,5,44,28,36,42,7,5,88,9,10};
length=sizeof(arr9)/
补充:软件开发 , C++ ,