排序算法代码汇总

来源:互联网 发布:python rsa 私钥加密 编辑:程序博客网 时间:2024/06/05 07:16

选择排序、快速排序、希尔排序、堆排序不是稳定的排序算法,
冒泡排序、插入排序、归并排序和基数排序是稳定的排序算法。

/* * Sort.cpp * *  Created on: 2014-10-24 *      Author: xiaohansong */#include <iostream>#include <cstdlib>#include <ctime>#include <cstring>#include <sys/timeb.h>using namespace std;void print(int*,int);//输出数组的每个元素void swap(int*, int*);//交换值void copy(int *, int *,int);//复制数组void insertSort(int*,int);void bubbleSort(int*,int);void selectionSort(int*,int);void quickSort(int*, int, int);void mergeSort(int*,int*, int, int);int main(){    struct timeb startTime , endTime;    srand(time(0));    cout << "请输入需要的随机数列的长度:";    int len;    cin >> len;    int *array = new int[len];    int *temp = new int[len];    //生成随机数列    for(int i = 0; i < len; i++){        array[i] = rand()%100;    }    //输出随机数列//  print(array,len);    cout << "插入排序:";    copy(array, temp,len);    ftime(&startTime);    insertSort(temp,len);    //print(temp,len);    ftime(&endTime);    cout << "运行时间:" << (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm) << "毫秒" << endl;    cout << "冒泡排序:";    copy(array, temp,len);    ftime(&startTime);    bubbleSort(temp,len);    ftime(&endTime);    cout << "运行时间:" << (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm) << "毫秒" << endl;    //print(temp,len);    cout << "选择排序:";    copy(array, temp,len);    ftime(&startTime);    selectionSort(temp,len);    ftime(&endTime);    cout << "运行时间:" << (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm) << "毫秒" << endl;    //print(temp,len);    cout << "快速排序:";    copy(array, temp,len);    ftime(&startTime);    quickSort(temp, 0, len-1);    ftime(&endTime);    cout << "运行时间:" << (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm) << "毫秒" << endl;    //print(temp,len);    cout << "归并排序:";    copy(array, temp, len);    ftime(&startTime);    mergeSort(array, temp, 0, len-1);    ftime(&endTime);    //print(array,len);    cout << "运行时间:" << (endTime.time-startTime.time)*1000 + (endTime.millitm - startTime.millitm) << "毫秒" << endl;    delete[] array;    delete[] temp;    return 0;}void print(int * array,int len){    for(int i = 0; i < len; i++){        cout << array[i] << " ";    }    cout << endl;}void copy(int *array, int *cp,int len){    for(int i = 0; i < len; i++){        cp[i] = array[i];    }}void swap(int *left, int *right){    int temp = *left;    *left = *right;    *right = temp;}void insertSort(int* arr, int len){    for(int curr = 1; curr < len; curr++){ //设定插入的次数,一般为n-1次,从第二个元素开始与其前面的元素比较        int key = arr[curr];        int pre = curr - 1;//pre指向当前key的前一个元素        while(pre >= 0 && key < arr[pre]){//判断key与前面元素的大小            arr[pre + 1] = arr[pre];//把大的元素往后推一格            pre--;//让pre指向前一个元素继续比较        }        arr[pre + 1] = key;//将key插入到其他比它小的元素前面    }}//冒泡排序两两交换,最多需要交换n-1次void bubbleSort(int* arr, int len){    int run = len;    bool flag = true;//设置标志,如果一趟没有发生交换,则说明已经排序完成    while(flag){        flag = false;        for(int i = 1; i < run; i++){            if(arr[i - 1] > arr[i]){//如果前面大于后面,则交换                swap(&arr[i-1], &arr[i]);                flag = true;            }        }        run--;    }}void selectionSort(int *arr, int len){    int min;    for(int i = 0; i < len; i++){        min = i;        for(int j = i+1; j < len; j++){//在未排序的部分寻找最小的元素放在当前排序位置            if(arr[min] > arr[j]){                min = j;            }        }        swap(&arr[i], &arr[min]);    }}void quickSort(int* arr, int left, int right){    //当区间小于5时,直接排序    if((right - left) <= 5){        insertSort(&arr[left], right - left + 1);        return;    }    int i, j , pivot;    pivot = arr[left];//用当前第一个元素作为基轴    i = left;    j = right;    if(left < right){        while(i < j){            while(i < j && arr[j] >= pivot){//从右往左找第一个小于pivot的数,找不到则会使j=i,直接退出                j--;            }            if(i < j){                arr[i++] = arr[j];//填数到左边,右边的j空出            }            while(i < j && arr[i] < pivot){// 从左向右找第一个大于等于pivot的数                i++;            }            if(i < j){                arr[j--] = arr[i];//填数到右边,左边的i空出            }        }        arr[i] = pivot;//最终i和j会相等        //递归        quickSort(arr, left, i - 1);        quickSort(arr, i + 1, right);    }}void mergeSort(int *arr, int* temp, int left, int right){    if((right - left) <= 5){        insertSort(&arr[left], right - left + 1);        return;    }    int i, j, k, mid = (left + right)/2;    mergeSort(arr, temp, left, mid);    mergeSort(arr, temp, mid+1, right);    //顺序拷贝左边的数组    for(i = mid; i >= left; i--)        temp[i] = arr[i];    //倒序拷贝右边的数组    for(j = 1; j <= right - mid; j++)        temp[right - j + 1] = arr[j + mid];    //合并两个有序数组    for(i = left, j = right, k = left; k <= right; k++)        if(temp[i] < temp[j])            arr[k] = temp[i++];        else            arr[k] = temp[j--];}
0 0
原创粉丝点击