zoukankan      html  css  js  c++  java
  • 一天一个小算法的学习之快速排序

      选择排序..排列的数组是arr[0]....arr[n-1],取出任意数据,作为关键数,将所有比它小的数都放该数前面,比它打的数放后面..(多个相同的值相对位置在算法结束时变动)

      代码:

    using System;
    
    namespace SortPra
    {
        public class QuickSort
        {
            public QuickSort ()
            {
            }
    
            private static int arrSize;
    
            public static int[] QuickWithSort (int[] array)
            {
                arrSize = array.Length;
                QuickSortVoid (array, 0, arrSize - 1);
                for (int i = 0; i < array.Length; i++) {
                    Console.WriteLine ("快速排序:" + array [i]);
                }
                return array;
            }
    
            static void QuickSortVoid (int[] array, int leftSize, int rightSize)
            {
                int i, j, s;
                if (leftSize < rightSize) {
                    i = leftSize - 1;
                    j = rightSize + 1;
                    s = array [(i + j) / 2];
    
                    //Console.WriteLine ("j = " + j);
                    Console.WriteLine ("s = " + s);
                    while (true) {
                        while (array [++i] < s)
                            Console.WriteLine ("array[i] = " + array [i]);
                        ;
                        while (array [--j] > s)
                            Console.WriteLine ("array[j] = " + array [j]);
                        ;
                        if (i >= j)
                            break;
                        Swap (ref array [i], ref array [j]);
                        
                    }
                    QuickSortVoid (array, leftSize, i - 1);
                    QuickSortVoid (array, j + 1, rightSize);
                }
            }
    
            static void Swap (ref int left, ref int right)
            {
                int temp;
                temp = left;
                left = right;
                right = temp;
            }
    
        }
    }
  • 相关阅读:
    Java生产环境线上栈故障排查问题(COPY)
    Java集合HashMap,List底层
    图算法--染色法判定二图
    图算法--kruskal
    图算法--最小生成树prim
    图算法--判负环
    图算法--floyd
    图算法--spfa
    图算法--bellman-ford (nm)
    图算法--堆优化版dijkstra
  • 原文地址:https://www.cnblogs.com/jbw752746541/p/8708248.html
Copyright © 2011-2022 走看看