现在的位置: 首页 > 综合 > 正文

快速排序算法c#

2012年10月27日 ⁄ 综合 ⁄ 共 1243字 ⁄ 字号 评论关闭
using System;
using System.Collections.Generic;
using System.Text;

namespace Sort
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
int[] array = 8,22,9,12,4,5,2,1,1};
            QuickSort(array, 
0, array.Length - 1);
            
for (int i = 0; i < array.Length; i++)
            
{
                Console.WriteLine(array[i]);
            }

            Console.Read();
        }


        
//快速排序从大到小
        private static void QuickSort(int[] sort, int start, int end)
        
{
            
int i = start;
            
int j = end + 1;

           
            
int val = sort[i];
            
do
            
{
                
do
                
{
                    i
++;
                }
 while (sort[i] > val && i < end);

                
do
                
{
                    j
--;
                }
 while (sort[j] < val && j > start);

                
if (i < j)
                
{
                    
int temp;
                    temp 
= sort[i]; sort[i] = sort[j]; sort[j] = temp;
                }

            }
 while (i < j);

            sort[start] 
= sort[j]; sort[j] = val;
            
if (j > start + 1) QuickSort(sort, start, j - 1);
            
if (j < end - 1) QuickSort(sort, j + 1, end);
        }

    }

}

抱歉!评论已关闭.