关于C++排序(选择排序、冒泡排序、插入排序、快速排序和合并排序)的一些学习心得

(1)选择排序
算法思想,第一次遍历先定位数组中最大数的值和位置(max_value,max_index),然后将该元素与数组末尾元素交换,之后遍历数组长度减1,继续上述操作,直到数组长度降为1

相应代码:
void selectsort(int list[], int size)
{
for (int i = size - 1; i >= 1; i--)
{
int max = list[0];
int index = 0;
for (int j = 1; j <= i; j++)
{
if (max < list[j])
{
max = list[j];
index = j;
}
}

if (index != i)
{
list[index] = list[i];
list[i] = max;
}
}
}
(2)冒泡排序
算法思想,首先第一次遍历,比较数组中list[0]与list[1]的大小,若list[1]
相应代码:
void bubblesort(int list[], int size)
{
for (int i = size - 1; i >= 1; i--)
{
for (int j = 0; j < i; j++)
{
if (list[j]>list[j + 1])
{
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
}
(3)插入排序
算法思想,首先以list[0]为第一个元素,取第二个元素list[1]为插入元素element,比较两元素,定位插入的位置,比较的同时将大于element的元素顺序后移,重复操作。
相应代码:
void insertsort(int list[], int size)
{
for (int i = 1; i < size; i++)
{
int element = list[i];
int k = 0;
for (k = i - 1; k >= 0 && list[k]>element; k--)
{
list[k + 1] = list[k];
}
list[k + 1] = element;
}

}
(4)快速排序
算法思想,首先->分划操作:定义一个中轴值pivot,这里简单地取数组第一个元素list[0]=pivot,然后定义两个指针low和high分别指向数组的第二个元素和最后一个元素,如果list[low]<=pivot,那么low指针向后移动,直到遇见list[low]>pivot,之后移动high指针,如果list[high]>pivot,指针向钱移动,直到遇见list[high]<=pivot的元素,然后交换list[high]和list[low]的位置。重复上述操作直到low>=high。
之后比较如果high>first并且list[high]>=pivot,那么high--,确定pivot和list[high]交换的位置。
最后如果实行了交换,返回high值,若未交换,返回first值。
之后进行快速排序,排序前判断first和last的值,如果last>frist,那么执行划分操作,返回中轴值的坐标,之后进行递归。
相应代码:
int partition(int list[], int first, int last)
{
int low = first+1;
int high = last;
int pivot = list[first];
while (high > low)
{
while (high >= low && list[low] <= pivot)
low++;
while (high >= low && list[high] > pivot)
high--;
if (high > low)
{
int temp = list[low];
list[low] = list[high];
list[high] = temp;
}
}

while (high > first && list[high] >= pivot)
high--;

if (list[high] < pivot)
{
list[first] = list[high];
list[high] = pivot;
return high;
}
else
{
return first;
}
}

void quicksort(int list[], int first, int last)
{
if (last > first)
{
int index = partition(list, first, last);
quicksort(list, first, index - 1);
quicksort(list, index + 1, last);
}
}

void quicksort(int list[], int size)
{
quicksort(list, 0, size - 1);
}
(5)合并排序
算法思想首先在于将两个数组list1和list2合并为一个有序数组temp,将两个数组的较小值放入temp中,存放晚辈得到一个递增的temp数组,之后设置一个数组拷贝函数,用于输入数组的分离和暂存拷贝,之后设置合并排序函数,先加判断size>1,如果数组空或者长度为1,就没必要排序了,然后在堆中申请一个size/2的内存,用于存放原数组前size/2个元素,之后递归调用合并排序函数,然后在堆中申请size-size/2的内存,用于存放原数组中后size-size/2个元素,之后递归调用合并排序函数,然后划分为两个数组后,在使用merge函数。
相应代码:
void merge(int list1[], int size1, int list2[], int size2, int temp[])
{
int index1 = 0;
int index2 = 0;
int index3 = 0;

while (index1 < size1 && index2 < size2)
{
if (list1[index1] <= list2[index2])
temp[index3++] = list1[index1++];
else
temp[index3++] = list2[index2++];
}
while (index1 < size1)
temp[index3++] = list1[index1++];
while (index2 < size2)
temp[index3++] = list2[index2++];
}

void copyarray(int source[], int sourcestart, int target[], int targetstart, int len)
{
for (int i = 0; i < len; i++)
target[targetstart + i] = source[sourcestart + i];
}

void mergesort(int list[], int size)
{
if (size > 1)
{
int *firsthalf = new int[size / 2];
copyarray(list, 0, firsthalf, 0, size / 2);
mergesort(firsthalf, size / 2);

int secondesize = size - size / 2;
int *secondhalf = new int[secondesize];
copyarray(list, size / 2, secondhalf, 0, secondesize);
mergesort(secondhalf, secondesize);

int *temp = new int[size];
merge(firsthalf, size / 2, secondhalf, secondesize, temp);
copyarray(temp, 0, list, 0, size);

delete[] temp;
delete[] firsthalf;
delete[] secondhalf;
}
}
这里只是我的个人对排序算法的理解,如果错误,请指正,感激不尽,谢谢,技术小白。

猜你喜欢

转载自blog.csdn.net/qq_27767337/article/details/80340970
今日推荐