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;
            }
    
        }
    }
  • 相关阅读:
    今天晚上有个什么样的博文呢
    STM8CubeMx来了
    开博啦
    Authentication
    文件上传设计要点
    分布式杂记
    SQL Server 知识集
    C# 集合使用误区
    网络知识集
    关于 elasticsearch 近实时特征的思考
  • 原文地址:https://www.cnblogs.com/jbw752746541/p/8708248.html
Copyright © 2011-2022 走看看