冒泡优化
int array[] = {3, 5, 1, 6, 8, 2, 4, 9, 7};
int count = sizeof(array) / sizeof(array[0]);
int flag = 0;
for (int i = 0; i < count - 1 && flag == 0; i++) {
flag = 1;
for (int j = 0; j < count - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = 0;
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
flag = 0;
}
}
}
for (int i = 0; i < count; i++) {
printf("[%2d] = %d\n", i, array[i]);
}
选择排序
for(int i = 0; i < count - 1; i++){
int minIndex= i;
for (int j = minIndex + 1; j < count; j++) {
if (array[minIndex] > array[j]) {
minIndex = j;
}
}
if(minIndex != i){
int temp = 0;
temp = array[minIndex];
array[minIndex] = array[i];
array[i] = temp;
}
}
for (int i = 0; i < count; i++) {
printf("[%2d] = %d\n", i, array[i]);
}
插入排序
int array[] = {3, 5, 1, 6, 8, 2, 4, 9, 7};
int count = sizeof(array) / sizeof(array[0]);
int temp = 0;
int j = 0;
for (int i = 1; i < count; i++) {
j = i;
temp = array[j];
while (j > 0 && temp < array[j - 1]) {
array[j] = array[j - 1];
j--;
}
array[j] = temp;
}
for(int i = 0; i < count; i++){
printf("[%2d] = %d\n", i, array[i]);
}
择半查找
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int count = sizeof(array) / sizeof(array[0]);
int start = 0, end = count - 1;
int mid =(start + end) / 2;
int target = 7;
while (start <= end) {
mid =(start + end) / 2;
if (array[mid] < target) {
start = mid + 1;
}else if(array[mid] > target){
end = mid - 1;
}else{
break;
}
}
if (start <= end) {
printf("[%2d] = %d\n", mid, array[mid]);
} else {
printf("not found\n");
}
打乱数组
for (int i = count - 1; i > 0 ; i--) {
unsigned int random = arc4random() % (i + 1);
int temp = 0;
temp = array[random];
array[random] = array[i];
array[i] = temp;
}
for (int i = 0; i < count; i++) {
printf("[%2d] = %d\n", i, array[i]);
}
索引
int array[10] = {8, 2, 4, 9, 3, 1, 6, 7, 0, 5};
int barry[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int count = sizeof(array) / sizeof(array[0]);
int minIndex = 0;
for (int i = 0; i < count - 1; i++) {
minIndex = i;
for (int j = minIndex + 1; j < count; j++) {
if (array[barry[minIndex]] > array[barry[j]]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = barry[minIndex];
barry[minIndex] = barry[i];
barry[i] = temp;
}
}
for (int i = 0; i < count; i++) {
printf("[%2d] = %d\n", i, barry[i]);
}
字符串排序
char names[3][30] = {"dingchao", "dulonglong", "xiexiangxin"};
int count = sizeof(names) / sizeof(names[0]);
int flog = 0;
for (int i = 0; i < count - 1 && flog == 0; i++) {
flog = 1;
for (int j = 0; j < count - i - 1; j++) {
if (strcmp(names[j], names[j + 1]) > 0) {
char temp[30] = {'\0'};
strcpy(temp, names[j]);
strcpy(names[j], names[j + 1]);
strcpy(names[j + 1], temp);
flog = 0;
}
}
}
for (int i = 0; i < count; i++) {
puts(names[i]);
}
所有的数组排序都用指针写一遍(可以借用语法糖);
冒泡排序
int array[] = {9, 5, 4, 7, 1, 3, 6, 2, 8};
int count = sizeof(array) / sizeof(*array);
int *p = array;
int flag = 0;
for (int i = 0; i < count - 1 && flag == 0; i++) {
flag = 1;
for (int j = 0; j < count - i - 1; j++) {
if (*(p + j) > *(p + j + 1)) {
int temp = 0;
temp = *(p + j);
*(p + j) = *(p + j + 1);
*(p + j + 1) = temp;
flag = 0;
}
}
}
for ( int i = 0; i < count; i++) {
printf("[%2d] = %d\n", i, *(p + i));
}
选择排序
int minIndex = 0;
for (int i = 0; i < count - 1; i++) {
minIndex = i;
for (int j = minIndex + 1; j < count; j++) {
if (*(array + minIndex) > *(array + j)) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = 0;
temp = *(array + minIndex);
*(array + minIndex) = *(array + i);
*(array + i) = temp;
}
}
for (int i = 0; i < count; i++) {
printf("[%2d] = %d\n", i, *(array + i));
}
插入排序
int j = 0;
int temp = 0;
for (int i = 1; i < count; i++) {
j = i;
temp = *(array + j);
while (j > 0 && temp < *(array + j - 1)) {
*(array + j) = *(array + j - 1);
j--;
}
*(array + j) = temp;
}
for (int i = 0; i < count; i++) {
printf("[%2d] = %d\n", i, *(array + i));
}
打乱数组
for (int i = count - 1; i > 0; i--) {
unsigned int random = arc4random() % (i + 1);
int temp = 0;
temp = *(array + random);
*(array + random) = *(array + i);
*(array + i) = temp;
}
for (int i = 0; i < count; i++) {
printf("[%2d] = %d", i, *(array + i));
}
择半查找
int array[] ={1, 2, 3, 4, 5, 6, 7, 8, 9};
int target = 7;
int start = 0, end = 0;
int mid = (start + end) / 2;
while (start <= end) {
mid =(start + end) / 2;
if (*(array + mid) < target) {
start = mid + 1;
}else if(*(array + mid) > target){
end = mid - 1;
}else{
break;
}
}
if (start <= end) {
printf("[%2d] = %d\n", mid, *(array + mid));
}
快速排序(递归)
void quickSort(int array[], int count)
{
if (count < 2) { //出口.递归的上面
return;
}
int start = 0, end = count - 1;
int temp = array[start];
while (start < end) {
while (start < end && array[end] > temp) {
end--;
}
if (start < end) {
array[start] = array[end];
start++;
}
while (start < end && array[start] < temp) {
start++;
}
if (start < end) {
array[end] = array[start];
end--;
}
}
array[start] = temp;
quickSort(array, start);
quickSort(array + start + 1, count - start - 1);
}