If tomorrow never comes

The meaning of life is creation,which is independent an boundless.

导航

快速排序(C#)

Posted on 2009-03-25 12:49  Brucegao  阅读(327)  评论(0编辑  收藏  举报

快速排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuickSort
{

    
public class QuickSort
        
{
            
public QuickSort()
            
{
            }


            
private void Swap(ref int i, ref int j)
            
//swap two integer 
            {
                
int t;
                t 
= i;
                i 
= j;
                j 
= t;
            }


            
public void Sort(int[] list, int low, int high)
            
{
                
if (high <= low)
                
{
                    
//only one element in array list 
                    
//so it do not need sort 
                    return;
                }

                
else if (high == low + 1)
                
{
                    
//means two elements in array list 
                    
//so we just compare them 
                    if (list[low] > list[high])
                    
{
                        
//exchange them 
                        Swap(ref list[low], ref list[high]);
                        
return;
                    }

                }

                
//more than 3 elements in the arrary list 
                
//begin QuickSort 
                myQuickSort(list, low, high);
            }


            
public void myQuickSort(int[] list, int low, int high)
            
{
                
if (low < high)
                
{
                    
int pivot = Partition(list, low, high);
                    myQuickSort(list, low, pivot 
- 1);
                    myQuickSort(list, pivot 
+ 1, high);
                }

            }


            
private int Partition(int[] list, int low, int high)
            
{
                
//get the pivot of the arrary list 
                int pivot;
                pivot 
= list[low];
                
while (low < high)
                
{
                    
while (low < high && list[high] >= pivot)
                    
{
                        high
--;
                    }

                    
if (low != high)
                    
{
                        Swap(
ref list[low], ref list[high]);
                        low
++;
                    }

                    
while (low < high && list[low] <= pivot)
                    
{
                        low
++;
                    }

                    
if (low != high)
                    
{
                        Swap(
ref list[low], ref list[high]);
                        high
--;
                    }

                }

                
return low;
            }


        }
 
    
class Program
    
{
        
static void Main(string[] args)
        
{
            QuickSort qu 
= new QuickSort();
            
int[] arr = new int[] 2334455667782113580 };
            qu.myQuickSort(arr, 
0, arr.Length - 1);

            Console.WriteLine(
"Data after QuickSort");
            
foreach (int i in arr)
            
{
                Console.Write(i 
+ ",");
            }

            Console.Read();
        }

    }

}