算法汇总

来源:互联网 发布:锡林浩特局气网络宾馆 编辑:程序博客网 时间:2024/06/11 08:37


//冒泡排序:
//#include <stdio.h>
//int main(int argc, const char * argv[])
//{
//    int array[] = {3, 5, 9, 4, 6, 7, 12, 24, 14};
//    int count = sizeof(array) / sizeof(array[0]);
//    int flag = 0;
//    for(int i = 0; 0 == flag && i < count; i++){
//        flag = 1;
//        for (int j = 0; j < count; j++) {
//            if(array[j] > array[j + 1]){
//                int temp = array[j + 1];
//                array[j + 1] = array[j];
//                array[j] = temp;
//                flag = 0;
//            }
//        }
//    }
//    for(int i = 0; i < count; i++){
//        printf("%d\n", array[i]);
//    }
//    return 0;
//}




//插入排序:
//#include <stdio.h>
//int main(int argc, const char * argv[])
//{
//    int array[] = {3, 5, 9, 4, 6, 7, 12, 24, 14};
//    int count = sizeof(array) / sizeof(array[0]);
//    for(int i = 0; i < count; i++){
//        int j = i;
//        int temp = array[i];
//        while(j > 0 && temp < array[j - 1]){
//            array[j] = array[j - 1];
//            j--;
//        }
//        array[j] = temp;
//    }
//    for (int i = 0; i < count; i++) {
//        printf("%d\n", array[i]);
//    }
//    return 0;
//}




//选择排序:
//#include <stdio.h>
//int main(int argc, const char * argv[])
//{
//    int array[] = {3, 5, 9, 4, 6, 7, 12, 24, 14};
//    int count = sizeof(array) / sizeof(array[0]);
//    
//    for(int i = 1; i < count; i++){
//        int minIndex = i;
//        for (int j = minIndex + 1; j < count ; j++) {
//            if(array[minIndex] > array[j]){
//                minIndex = j;
//            }
//        }
//        if(minIndex != i){
//            int temp = array[i];
//            array[i] = array[minIndex];
//            array[minIndex] = temp;
//        }
//    }
//    for (int i = 0; i < count ; i++) {
//        printf("%d\n", array[i]);
//    }
//    return 0;
//}




//折半查找:
//#include <stdio.h>
//int main(int argc, const char * argv[])
//{
//    int array[] = {2, 5, 6, 7, 9, 12, 24, 26, 28};
//    int count = sizeof(array) / sizeof(array[0]);
//    
//    int target = 12;
//    int start = 0;
//    int end = count - 1;
//    int mid = (start + end) / 2;
//  
//        
//        while(start <= end){
//            mid = (start + end) / 2;
//            if(array[mid] > target){
//                end = mid - 1;
//            }else if(array[mid] < target){
//                start = mid + 1;
//            }else{
//                break;
//            }
//        }
//   
//    if (start <= end) {
//        printf("target 的位置是:%d\n", mid + 1);
//    }else{
//        printf("Not found.");
//    }
//    return 0;
//}




//打乱数组:
//#include <stdio.h>
//#include <stdlib.h>
//int main(int argc, const char * argv[]){
//    int array[] = {2, 5, 6, 7, 9, 12, 24, 26, 28};
//    int count = sizeof(array) / sizeof(array[0]);
//    for (int i = 0; i < count; i++) {
//        unsigned int random = arc4random() % (i + 1);
//        int temp = array[random];
//        array[random] = array[i];
//        array[i] = temp;
//    }
//    for(int i = 0; i < count; i++ ){
//        printf("%3d\n", array[i]);
//    }
//    
//    return 0;
//}




//快速排序:
//#include <stdio.h>
//
//void quickSort(int array[], int count);
//
//int main(int argc, const char * argv[])
//{
//    int array [] = {1, 3, 5, 8, 9, 7, 2, 6, 4};
//    int count = sizeof(array) / sizeof(array[0]);
//    
//    quickSort(array, count);
//    
//    for (int i = 0; i < count; i++) {
//        printf("%d\n", array[i]);
//    }
//}
//void quickSort(int array[], int count)
//{
//    if (count < 2) {
//        return;
//    }
//    int start = 0;
//    int end = count - 1;
//    int temp = array[start];
//    while (start < end) {
//        while (start < end && temp < array[end]) {
//            end--;
//        }
//        if (start < end) {
//            array[start] = array[end];
//            start++;
//        }
//        while (start < end && temp > array[start]) {
//            start++;
//        }
//        if (start < end) {
//            array[end] = array[start];
//            end--;
//        }
//        
//    }
//    array[start] = temp;
//    quickSort(array, start);
//    quickSort(array + start + 1, count - start - 1);
//    
//}
//
//#include <stdio.h>
//int main(int argc, const char * argv[])
//{
//    int array[] = {1, 3, 5, 8, 9, 7, 2, 6, 4};
//    int count = sizeof(array) / sizeof(array[0]);
//    
//    for (int i = 0; i < count; i++) {
//        printf("%d\n", array[i]);
//    }
//    
//    return 0;
//}
//

















0 0