1 冒泡排序
1 冒泡排序:
void bubbleSort(int *data,int start,int end)
{
if (start < end) {
int temp = 0;
int length = end - start + 1;
for (int i = start; i < length - 1; i ++) {
if (data[i] < data[i + 1]) {
temp = data[i];
data[i] = data[i + 1];
data[i + 1] = temp;
}
}
end --;
bubbleSort(data,start,end);
}
}
注意问题:end--那里,注意每次递归的起始终止下标的移动,起始下标不变,终止下标每次减1,
循环结束条件 为start == end,以及i < length - 1;
2 选择排序
void selectionSort(int *data,int start,int end)
{
if (start < end)
{
//int length = end - start + 1;
int temp = data[start];
int index = start;
for (int i = start + 1; i < end + 1; i ++) {
if (data[index] > data[i]) {
index = i;
}
}
for (int i = 0; i < 10; i ++) {
printf("%d ",data[i]);
}
printf("\n");
if (start != index) {
temp = data[start];
data[start] = data[index];
data[index] = temp;
}
start ++;
selectionSort(data, start, end);
}
}
注意问题:start ++那里,注意每次递归的起始终止下标的移动,起始每次加1,终止下标不变,
循环结束条件 为start == end,以及i < end + 1