排序

来源:互联网 发布:开源股票软件 python 编辑:程序博客网 时间:2024/05/27 20:19

开始:

开发中常用到的排序有:

  • 冒泡排序
  • 选择排序
  • 快速排序
  • 插入排序
  • 堆排序
  • 归并排序
  • 希尔排序
  • 基数排序

继续

1. 冒泡排序

基本思想:(升序为例)
1. n个元素组成的无序序列,原则上进行n-1次排序。每一趟,从第一个数开始,依次比较前一个元素与后一个元素大小;
2. 若前一个元素比后一个元素大,则交换两个元素的位置,第一趟之后,最大的数会出现在无序序列最末端(成为有序序列);
3. 去掉有序序列,将剩下的无序序列进行比较,重复n-1趟完成排序

冒泡排序会重复比较数组中相邻的两个元素;若一个元素比另一个元素大(小),则这两个元素交换位置;重复比较直至最后一个元素。过程中比较会重复n-1趟,每趟比较n-j次(j是已经排序好的元素个数);每一趟比较都能找出无序序列中最大或最小的元素,这样就如同书跑一样从水底逐个冒出水面。

Objective-C写法:
#pragma mark - 冒泡升序排序- (void)sortByBubbleAscendingWithArray:(NSMutableArray *)array{    for (int i = 1; i < array.count; i++) {        for (int j = 1; j < array.count - i;j++) {            if (([array[j] compare:array[j+1]]) == NSOrderedDescending) {                [array exchangeObjectAtIndex:i withObjectAtIndex:j];            }        }    }    NSLog(@"冒泡升序结果:%@", array);}调用:[self sortByBubbleAscendingWithArray:[NSMutableArray arrayWithObjects:@1,@3,@5,@7,@9,@(2),@(4),@(6),@(8),@(10), nil]];
Java写法:
public class SortByBubble {    public static void main(String[] args) {        int array[] = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };        for (int i = 0; i < array.length - 1; i++) {            // 趟数:最多做n-1趟排序            for (int j = 0; j < array.length - i - 1; j++) {                 // 对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)                if (array[j] < array[j + 1]) {                    // 把小的值交换到后面                    int temp = array[j];                    array[j] = array[j + 1];                    array[j + 1] = temp;                }            }            System.out.print("第" + (i + 1) + "次排序结果:");            for (int a = 0; a < array.length; a++) {                System.out.print(array[a] + "\t");            }            System.out.println("");        }        System.out.print("最终排序结果:");        for (int a = 0; a < array.length; a++) {            System.out.print(array[a] + "\t");        }    }}

2. 选择排序:

基本思想:
1. n个元素的无序序列,第一趟遍历n个数据,找出其中最小的数值与第一个元素交换
2. 第二趟遍历剩下的n-1个数据,找出其中最小的数值与第二个元素交换……第n-1趟遍历剩下的2个数据,找出其中最小的数值与第n-1个元素交换,至此选择排序完成。

Objective-C写法:
- (void)sortBySelectionAscendingWithArray:(NSMutableArray *)array{    for (int i = 0; i < array.count; i ++) {        for (int j = i + 1; j < array.count; j ++) {            if ([array[i] integerValue] > [array[j] integerValue]) {                int temp = [array[i] intValue];                array[i] = array[j];                array[j] = [NSNumber numberWithInt:temp];            }        }    }    NSLog(@"选择升序结果:%@", array);}

Java写法:

public class SortBySelection {    public static void main(String[] args) {        int[] array = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };        System.out.println("最开始的数组:");        for (int num : array) {            System.out.print(num + " ");        }        // 选择排序的优化        for (int i = 0; i < array.length - 1; i++) {            // 做第i趟排序            int k = i;            for (int j = k + 1; j < array.length; j++) {                // 选最小的记录                if (array[j] < array[k]) {                    // 记下目前找到的最小值所在的位置                    k = j;                 }            }            // 在内层循环结束,也就是找到本轮循环的最小的数以后,再进行交换            if (i != k) { // 交换a[i]和a[k]                int temp = array[i];                array[i] = array[k];                array[k] = temp;            }            System.out.print("第" + (i + 1) + "次排序结果:");            for (int a = 0; a < array.length; a++) {                System.out.print(array[a] + "\t");            }            System.out.println("");        }        System.out.println();        System.out.println("最终排序结果:");        for (int num : array) {            System.out.print(num + " ");        }    }}

3. 快速排序

基本思想:
在序列中选择一个标杆,通常选择序列的第一个数,然后对序列中的数从两头开始,将小于他的数放在左边,大于他的数放在他右边。这时以这个数为标杆,分成了左右两块,左边的都小于他,右边的都大于他。然后再递归调用,直到没有可排序的。即只有两个元素。

Objective-C写法:
#pragma mark - 快速升序排序- (void)sortByQuickAscendingWithArray:(NSMutableArray *)array leftIndex:(NSInteger)left rightIndex:(NSInteger)right{    if (left < right) {        NSInteger temp = [self getMiddleIndex:array leftIndex:left rightIndex:right];        [self sortByQuickAscendingWithArray:array leftIndex:left rightIndex:temp - 1];        [self sortByQuickAscendingWithArray:array leftIndex:temp + 1 rightIndex:right];    }    NSLog(@"快速升序结果:%@", array);}- (NSInteger)getMiddleIndex:(NSMutableArray *)array leftIndex:(NSInteger)left rightIndex:(NSInteger)right{    NSInteger tempValue = [array[left] integerValue];    while (left < right) {        while (left < right && tempValue <= [array[right] integerValue]) {            right --;        }        if (left < right) {            array[left] = array[right];        }        while (left < right && [array[left] integerValue] <= tempValue) {            left ++;        }        if (left < right) {            array[right] = array[left];        }    }    array[left] = [NSNumber numberWithInteger:tempValue];    return left;}
Java写法:
public class SortByQuick {    public void quick_sort(int[] arrays, int lenght) {        if (null == arrays || lenght < 1) {            System.out.println("input error!");            return;        }        _quick_sort(arrays, 0, lenght - 1);    }    public void _quick_sort(int[] arrays, int start, int end) {        if (start >= end) {            return;        }        int i = start;        int j = end;        int value = arrays[i];        boolean flag = true;        while (i != j) {            if (flag) {                if (value > arrays[j]) {                    swap(arrays, i, j);                    flag = false;                } else {                    j--;                }            } else {                if (value < arrays[i]) {                    swap(arrays, i, j);                    flag = true;                } else {                    i++;                }            }        }        snp(arrays);        _quick_sort(arrays, start, j - 1);        _quick_sort(arrays, i + 1, end);    }    public void snp(int[] arrays) {        for (int i = 0; i < arrays.length; i++) {            System.out.print(arrays[i] + " ");        }        System.out.println();    }    private void swap(int[] arrays, int i, int j) {        int temp;        temp = arrays[i];        arrays[i] = arrays[j];        arrays[j] = temp;    }    public static void main(String[] args) {        SortByQuick q = new SortByQuick();        int[] a = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };        q.quick_sort(a, 9);    }}

4. 插入排序

基本思想
插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕。

Objective-C写法:
#pragma mark - 插入升序排序- (void)sortByInsertionAscending:(NSMutableArray *)array{    for (NSInteger i = 1; i < array.count; i ++) {        NSInteger temp = [array[i] integerValue];        for (NSInteger j = i - 1; j >= 0 && temp < [array[j] integerValue]; j --) {            array[j + 1] = array[j];            array[j] = [NSNumber numberWithInteger:temp];        }    }    NSLog(@"插入升序结果:%@",array);}
Java写法:
public class SortByInsertion {     private static String readLine;    static void insertion_sort(int[] unsorted){         for (int i = 1; i < unsorted.length; i++){             if (unsorted[i - 1] > unsorted[i]){                 int temp = unsorted[i];                 int j = i;                 while (j > 0 && unsorted[j - 1] > temp){                     unsorted[j] = unsorted[j - 1];                     j--;                 }                 unsorted[j] = temp;             }         }     }     public static void main(String[] args) {         int[] x = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };         insertion_sort(x);        System.out.println("最终排序结果:");        for (int num : x) {            System.out.print(num + " ");        }     }}
原创粉丝点击