选择排序和快速排序性能比较

这里为了不修改我之前的文章,重新贴一下之前的代码

//selectSort && quickSort comparison
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

int partition(int a[], int idxLeft, int idxRight, int idxPivot)
{
    int i, idxCur = idxLeft;

    int pivot = a[idxPivot];
    swap(&a[idxPivot], &a[idxRight]);

    for(i = idxLeft; i < idxRight; i++){
        if(a[i] <= pivot){
            swap(&a[i], &a[idxCur]);
            idxCur++;
        }
    }

    swap(&a[idxCur], &a[idxRight]);
    return idxCur;
}

void quicksort(int a[], int idxLeft, int idxRight)
{
    if(idxLeft >= idxRight){
        return;
    }

    int idxPivot;
    idxPivot = partition(a, idxLeft, idxRight, (idxRight + idxLeft)/2);

    quicksort(a, idxLeft, idxPivot - 1);
    quicksort(a, idxPivot + 1, idxRight);
}

void selectSort(int a[], int size)
{
    int i, j, aux;//auxiliary
    for(i = 0; i< size - 1; i++){
        aux = i;
        for(j = i + 1; j < size; j++){
            if(a[aux] > a[j]){
                aux = j;
            }
        }
        if(i != aux){
            swap(&a[i], &a[aux]);
        }
    }
}

#define DEBUG_SORTx
int main(int argc, char **argv)
{
    if(argc != 2){
        printf("\tusage : ./test SIZE\n");
        return -1;
    }
    //the larger the SIZE, the better of the quick sort
    int SIZE = atoi(argv[1]);
    int a[SIZE], i, b[SIZE];
    struct timeval tv1, tv2, tv3;

    srand(time(NULL)^getpid());

    for(i = 0; i < SIZE; i++){
        a[i] = rand() % 1000;
    }
    a[0] = 73, a[1] = 97, a[2]= 26;
    memcpy(b, a, sizeof(a));

    int size = sizeof(a)/sizeof(int);

    gettimeofday(&tv1, NULL);      //(2)

    selectSort(a, size);

    gettimeofday(&tv2, NULL);

    printf("s : %.3lf ms\n", (tv2.tv_usec - tv1.tv_usec) * 1.0 / 1000 + (tv2.tv_sec - tv1.tv_sec) * 1000);

    quicksort(b, 0, size - 1);
    gettimeofday(&tv3, NULL);

    printf("q : %.3lf ms\n", (tv3.tv_usec - tv2.tv_usec) * 1.0 / 1000 + (tv3.tv_sec - tv2.tv_sec) * 1000);

#ifdef DEBUG_SORT
    for(i = 0; i< size; i++){
        printf("%d\t", a[i]);
    }
    printf("\n");

    for(i = 0; i< size; i++){
        printf("%d\t", b[i]);
    }
    printf("\n");
#endif

    return 0;
}

运行./out 100000

输出结果:

s : 11630.885 ms

q : 42.877 ms

10万条数据排序,二者性能已经相当明显。选择排序使用了11.6s,而快速排序只用了42.9ms

时间: 2024-08-27 04:56:32

选择排序和快速排序性能比较的相关文章

七大内部排序算法总结(插入排序、希尔排序、冒泡排序、简单选择排序、快速排序、归并排序、堆排序)

 写在前面: 排序是计算机程序设计中的一种重要操作,它的功能是将一个数据元素的任意序列,重新排列成一个按关键字有序的序列.因此排序掌握各种排序算法非常重要.对下面介绍的各个排序,我们假定所有排序的关键字都是整数.对传入函数的参数默认是已经检查好了的.只是简单的描述各个算法并给出了具体实现代码,并未做其他深究探讨. 基础知识: 由于待排序的记录数量不同,使得排序过程中设计的存储器不同,可将排序方法分为两大类:一类是内部排序,指的是待排序记录存放在计算机随机存储器中进行的排序过程.另一类是外部排序,

冒泡排序,选择排序,快速排序

package com.hello; public class HelloJava { /** * 冒泡排序(通过一次一次的循环,根据相近两个值进行比较,将大的值往下移) * @author MR ZHANG * @param arr * @return */ public static void getBubbleSort(int[] arr){ for(int i = 1;i< arr.length-1;i++){ for(int j=0; j< arr.length-i;j++){ if

几种排序方式的java实现(01:插入排序,冒泡排序,选择排序,快速排序)

以下为集中排序的java代码实现(部分是在引用别人代码): 插入排序(InsertSort): //代码原理 public static void iSort(int[] a){ for(int i = 1;i < a.length; i++){ if(a[i] < a[i-1]){ int temp = a[i]; while(temp < a[i-1]){ a[i] = a[i-1]; if(i-1 > 0){ i--; }else{ break; } } a[i-1] = t

九种经典排序算法详解(冒泡排序,插入排序,选择排序,快速排序,归并排序,堆排序,计数排序,桶排序,基数排序)

综述 最近复习了各种排序算法,记录了一下学习总结和心得,希望对大家能有所帮助.本文介绍了冒泡排序.插入排序.选择排序.快速排序.归并排序.堆排序.计数排序.桶排序.基数排序9种经典的排序算法.针对每种排序算法分析了算法的主要思路,每个算法都附上了伪代码和C++实现. 算法分类 原地排序(in-place):没有使用辅助数据结构来存储中间结果的排序**算法. 非原地排序(not-in-place / out-of-place):使用了辅助数据结构来存储中间结果的排序算法 稳定排序:数列值(key)

基本排序算法(冒泡排序 选择排序 插入排序 快速排序 归并排序 基数排序 希尔排序)

冒泡排序 public static void bubbleSort(int[] arr){ int lgn = arr.length; for (int i = 0; i < lgn - 1; i++) { for (int j = 0; j < lgn - 1 - i; j++) { if(arr[j] > arr[j + 1]){ int temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } } } } 选择排序 publ

经典排序算法回顾:选择排序,快速排序

//选择排序基本思想就是:一个一个最值查找,然后排序 //the simple insertSortWay void selectSort(int *a){ int n = strlen(a); for(int k; k<n; k++){ int l = k; for(int j; j<k; j++){ if(a[j] > a[l]){ l = j; } } int tmp = a[k]; a[k] = a[l]; a[l] = tmp; } } //the nice insertSor

【数据结构】常用排序算法(包括:选择排序,堆排序,冒泡排序,选择排序,快速排序,归并排序)

直接插入排序: 在序列中,假设升序排序 1)从0处开始. 1)若走到begin =3处,将begin处元素保存给tmp,比较tmp处的元素与begin--处元素大小关系,若begin处<begin-1处,将begin-1处元素移动到begin:若大于,则不变化.再用tmp去和begin--处的元素用同样的方法去作比较,直至begin此时减少到数组起始坐标0之前结束. 3)以此类推,依次走完序列. 时间复杂度:O() 代码如下: //Sequence in ascending order  voi

排序:正序冒泡,交错冒泡,插入排序,选择排序,快速排序

1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<time.h> 4 #include<string.h> 5 const int N=10; //修改随机数据量 6 bool onOroff=1; //数组打印开关:1要打印,0不打印 7 void bubble_sort(int a[],int n) 8 { 9 for(int i=0 ; i<N ; ++i) 10 { 11 for(int j

选择排序 //插入排序 //快速排序

-(void)selectSortWithArray:(NSArray *)aData{ NSMutableArray *data = [[NSMutableArray alloc]initWithArray:aData]; for (int i=0; i<[data count]-1; i++) { int m =i; for (int j =i+1; j<[data count]; j++) { if ([data objectAtIndex:j] < [data objectAtI