排序算法学习,记录之。
最佳情况:T(n) = O(n)
最差情况:T(n) = O(n2)
平均情况:T(n) = O(n2)
int Bubble_Sort(int *array,int len)
{
if (len == 0) return 0;
int i, j;
for (i = 0; i < len; i++)
{
for (j = i+1; j < len; j++)
{
if (array[i] > array[j])
{
int temp;
temp = array[j];
array[j] = array[i];
array[i] = temp;
}
}
}
return *array;
}
最佳情况:T(n) = O(n2)
最差情况:T(n) = O(n2)
平均情况:T(n) = O(n2)
int Select_Sort(int *array, int len)
{
if (len == 0) return 0;
int i,j;
for (i = 0; i < len; i++)
{
int min = array[i];
int Index = i;
for (j = i+1; j < len; j++)
{
if (array[j] < min)
{
min = array[j];
Index = j;
}
}
array[Index] = array[i];
array[i] = min;
}
return *array;
}
最佳情况:T(n) = O(n)
最坏情况:T(n) = O(n2)
平均情况:T(n) = O(n2)
int Insert_Sort(int *array, int len)
{
if (len == 0) return 0;
for (int i = 0; i < len - 1; i++)
{
int preIndex = i;
int current = array[i + 1];
while (preIndex >= 0 && current < array[preIndex])
{
array[preIndex + 1] = array[preIndex];
preIndex--;
}
array[preIndex + 1] = current;
}
return *array;
}
最佳情况:T(n) = O(nlog2 n)
最坏情况:T(n) = O(nlog2 n)
平均情况:T(n) =O(nlog2n)
int Shell_Sort(int *array, int len)
{
if (len == 0) return 0;
int gap = len / 2;
while (gap > 0)
{
for (int i = 0; i < len - gap; i++)
{
int preIndex = i;
int current = array[i+gap];
while (preIndex >= 0 && current < array[preIndex])
{
array[preIndex+gap] = array[preIndex];
preIndex = preIndex - gap;
}
array[preIndex+gap] = current;
}
gap = gap / 2;
}
return *array;
}
最佳情况:T(n) = O(nlogn)
最差情况:T(n) = O(n2)
平均情况:T(n) = O(nlogn)
//快速排序
int GetIndex(int *arr, int left, int right)
{
if (left > right) return 0;
int curent = arr[left];
while (left < right)
{
while (left < right && curent <= arr[right])
right--;
arr[left] = arr[right];//挖坑 vec[right]
while (left < right && curent >= arr[left])
left++;
arr[right] = arr[left];//填坑 vec[right] 挖坑 vec[left]
}
arr[left] = curent;//填坑 vec[left]
return left;
}
void Quick_Sort(int *arr,int left,int right)
{
if (left > right) return;
if (left < right)
{
int index = GetIndex(arr, left, right);
Quick_Sort(arr, 0, index - 1);
Quick_Sort(arr, index + 1, right);
}
}
最佳情况:T(n) = O(n)
最差情况:T(n) = O(nlogn)
平均情况:T(n) = O(nlogn)
void Merge(int *array, int left, int mid, int right)
{
int size = right - left + 1;
//int *array1 = (int *)malloc(size * sizeof(int));
int *array1 = new int[size];
memset(array1, 0, size * 4);
int i = left, j = mid + 1;
int Index = 0;
while (i <= mid && j <= right)
{
array1[Index++] = array[i] < array[j] ? array[i++] : array[j++];
}
while (i <= mid)
{
array1[Index++] = array[i++];
}
while (j <= right)
{
array1[Index++] = array[j++];
}
for (int n = 0; n < size; n++)
{
array[left++] = array1[n];
}
delete array1;
}
void Merge_Sort(int *array, int left,int right)
{
if (left == right) return;
int mid = left + ((right - left) / 2);
Merge_Sort(array, left, mid);
Merge_Sort(array, mid+1, right);
Merge(array, left, mid, right);
}
最佳情况:T(n) = O(nlogn)
最差情况:T(n) = O(nlogn)
平均情况:T(n) = O(nlogn)
//堆排序
void heap_Adjust(int *arr, int i, int length)
{
//int temp = arr[i];
for(int left = 2 * i + 1; left < length; left = 2 * left + 1)
{
int right = left + 1;
if (right < length && arr[left] < arr[right])//如果右节点大于左节点
{
left++;
}
if (arr[i] < arr[left])
{
// arr[i] = arr[left];
swap(arr, i, left);
i = left;
}
else {
break;
}
// arr[i] = temp;
}
}
void heap_Sort(int *arr, int length)
{
if (length == 0) return;
for (int i = (length / 2) - 1 ; i >= 0; i--)
{
heap_Adjust(arr,i,length);
}
while (length > 0)
{
swap(arr, 0, length-1);
length--;
heap_Adjust(arr, 0,length);
}
}
最佳情况:T(n) = O(n+k)
最差情况:T(n) = O(n+k)
平均情况:T(n) = O(n+k)
int Count_Sort(int *array,int len)
{
if (len == 0) return 0;
int min = array[0];
int max = array[0];
for (int i = 1; i < len; i++)
{
if (array[i] < min)
{
min = array[i];
}
if (array[i] > max)
{
max = array[i];
}
}
int size = max - min + 1;
int *array1 = new int[size];
//int *array1 = (int *)malloc(size * sizeof(int));
memset(array1, 0, size * 4);
for (int i = 0; i < len; i++)
{
array1[array[i]-min]++;
}
int j = 0;
for (int i = 0; i < size; i++)
{
while (array1[i]--)
{
array[j++] = i + min;
}
}
return *array;
delete array1;
}
int main()
{
int A[] = { 2, 6, 4, 8, 2, 5, 9,9,10,20 };
int len = sizeof(A) / sizeof(int);
//Bubble_Sort(A,len);
//Select_Sort(A, len);
//Insert_Sort(A, len);
//Shell_Sort(A,len);
//Merge_Sort(A, 0, len-1);
//Quick_Sort(A,1,len - 1);
//heap_Sort(A,len-1);
//Count_Sort(A, len);
for (int i = 0; i < len; i++)
{
printf("%d ", A[i]);
}
return 0;
}
以上是排序算法的简单实现,算法原理的理解敬请各位移步如下:
该链接的这位老哥总结的非常详细,还有算法过程的动态图图解,值得珍藏。