快速排序QuickSort

本文详细介绍了快速排序算法的基本原理、步骤,并通过C#语言实现了该算法。文章包括算法介绍、具体实现代码及示例运行结果,旨在帮助读者理解和掌握快速排序的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Introduction

Quicksort is basically work on divide and conquer algorithm. Firstly quicksort divide the complete large array into small sub array, the low elements and the high elements. Quicksort also known as partition-exchange sort, use these steps:

  1. Choose any element of the array to be the pivot.
  2. Divide all others elements into two partition.

    • All Elements less than the pivot must be first partition.
    • All elements greater than the pivot must be the second partition.

  3. Use the recursion to sort both partition.
  4. Join the first sorted partition, the pivot, and the second sorted partition.

Algorithm

Quicksort(a,p,r)

  1. If p<r
  2. q=partition(a,p,r)
  3. Quicksort(a,p,q-1)
  4. Quicksort(a,q+1,r)
  5. Exit

Partition(a,p,r)

  1. X=a[r]
  2. I=p-1
  3. J=p to r-1
  4. Do if a[j]<=x
    I=i+1
    Exchange a[i]=a[j]
  5. A[i+1]=a[j]
  6. Return i+1

Quicksort 

Program

Now place the following code in you console app page:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace array  
  8. {  
  9.         class Program  
  10.         {  
  11.            static public int partition(int[] item, int left, int right)  
  12.             {  
  13.                 int pivot = item[left];  
  14.                 while (true)  
  15.                 {  
  16.                     while (item[left] < pivot)  
  17.                         left++;  
  18.   
  19.                     while (item[right] > pivot)  
  20.                         right--;  
  21.   
  22.   
  23.                     if (left < right)  
  24.                     {  
  25.                         int tem;  
  26.                         tem = item[left];  
  27.                         item[left] = item[right];  
  28.                         item[right] = tem;  
  29.                     }  
  30.                     else  
  31.                     {  
  32.                         return right;  
  33.                     }  
  34.                 }  
  35.             }  
  36.            static public void quicksort(int [] arr,int left,int right)  
  37.             {  
  38.             if (left<right)  
  39.             {  
  40.                 int pivot = partition(arr,left, right);  
  41.                 if (pivot>1)  
  42.                 {  
  43.                     quicksort(arr,left,pivot-1);  
  44.                 }  
  45.                 if (pivot+1<right)  
  46.                 {  
  47.                     quicksort(arr,pivot+1,right);  
  48.                 }  
  49.             }  
  50.             }  
  51.             static void Main(string[] args)  
  52.             {  
  53.                 int [] item=new int[10];  
  54.                Console.WriteLine("Enter 10 Element");  
  55.                 for (int i = 0; i < 10; i++)  
  56.             {  
  57.             item[i]=Int32.Parse(Console.ReadLine());  
  58.             }  
  59.                 quicksort(item,0,10-1);  
  60.                 Console.WriteLine("\nSorted Array :");  
  61.                 for (int j  = 0; j  < 10; j ++)  
  62.             {  
  63.              Console.WriteLine(+item[j]);  
  64.             }  
  65.                 Console.ReadLine();  
  66.             }  
  67.         }  
  68.     }  
Output

output of Quilsort
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值