面试常用排序算法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tianxieeryang/article/details/86597837

面试常用排序算法

代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Bubble sort  (O(n^2))
void BubbleSort(int* a, int len) {
    int i, j,  temp;
    for (i = 0; i < len - 1; i++) {
        for (j = i + 1; j < len; j++) {
            if (*(a + i) > *(a + j)) {
                temp = *(a + j);
                *(a + j) = *(a + i);
                *(a + i) = temp;
            }
        }
    }
}

// quick sort   (O(n*logn))
void Quick_Sort(int* a, int low, int high) {
    if ( high < low)    return ;
    int temp = *(a + low);
    int left = low;
    int right = high;
    while (left < right) {
        while (left < right && temp <= *(a + right)) {
            right--;
        }
        if (left < right) {
            *(a + left) = *(a + right);
            //left++;
        }
        while (left < right && temp >= *(a + left)) {
            left++;
        }
        if (left < right) {
            *(a + right) = *(a + left);
            //right--;
        }
    }
    *(a + left) = temp;
    Quick_Sort(a, low, right - 1);
    Quick_Sort(a, right + 1, high);

}

// Straight select sort   (O(n^2))
void Straight_Select_Sort(int* a, int len) {
    int i, j, min, temp, min_arr;
    for (i = 0; i < len; i++) {
        min = i;
        for (j = i; j < len; j++) {
            if (*(a + j) < *(a + min)) {
                min = j;
            }
        }
        if (min != i) {
            temp = *(a + i);
            *(a + i) = *(a + min);
            *(a + min) = temp;
        }
    }
}

// heap sort    (O(n*logn))
// 升序,大顶堆
void Heap_Swap(int* a, int n1, int n2) {
    int temp = *(a + n1);
    *(a + n1) = *(a + n2);
    *(a + n2) = temp;
}
void Heap_Adjust(int* a, int num, int size) {
    int left = 2 * num + 1;     // left mode
    int right = 2 * num + 2;    // right node
    int max = num;      // this node
    if (left < size && *(a + max) < *(a + left)) {
        max = left;
    }
    if (right < size && *(a + max) < *(a + right)) {
        max = right;
    }
    if (max != num) {     // rescursive downaward adjust
        Heap_Swap(a, num, max);
        Heap_Adjust(a, max, size);
    }
}
void Heap_Sort(int* a, int len) {
    int i;
    // build big heap
    for (i = len/2 - 1; i >= 0; i--) {
        Heap_Adjust(a, i, len);
    }
    // adjust to end
    for (; len > 0; len--) {
        Heap_Swap(a, 0, len);
        Heap_Adjust(a, 0, len);
    }
}

// shell sort
void swap(int* a, int n1, int n2) {
    int temp = *(a + n1);
    *(a + n1) = *(a + n2);
    *(a + n2) = temp;
}
void Shell_Sort(int* a, int len) {
    int i, j;
    for (i = len/2; i > 0; i/=2) {
        for (j = i; j < len; j++) {
            int temp = j;
            while (temp >= i && *(a + temp) < *(a + temp - i)) {
                swap (a, temp, temp - i);
                temp -= i;
            }
        }
    }
}


int main()
{
    int a[10] = {4, 5, 2, 7, 8, 10, 1, 9, 3, 6};
    int length = 10;

    //BubbleSort(a, length);
    //Quick_Sort(a, 0, length - 1);
    //Straight_Select_Sort(a, length);
    //Heap_Sort(a, length);
    Shell_Sort(a, length);

    int i;
    printf("a[10] = ");
    for (i = 0; i < length; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}                                

猜你喜欢

转载自blog.csdn.net/tianxieeryang/article/details/86597837
今日推荐