?? 隨機快排.txt
字號:
#include <iostream>
#include <ctime>
using namespace std ;
#define N 20
int Partition(int a[], int low,int high)//劃分a[m]...a[p]
{
int i = low,j = high ,temp = a[low] ;
while(i<j)
{
while(i<j && temp <= a[j]) j --;
if(i < j)
{
a[i] = a[j] ;
i ++ ;
}
while(i<j && a[i] < temp) i++ ;
if(i < j)
{
a[j] = a[i] ;
j -- ;
}
}
a[i] = temp;
return i ;
}
void quickSort(int a[], int low,int high)//////隨機快排序
{
if(low < high )
{
if((high-low)>5) swap(a[rand()%(high-low+1) + low] ,a[low]) ;
int i = Partition( a, low,high);
quickSort(a,low,i - 1);
quickSort(a,i+1,high);
}
}
int selection(int a[] , int low , int high , int k )//選擇第幾小的元素
{
int s = Partition(a,low,high ) ;
if(s == k-1) return a[s] ;
else if(s < k-1) return selection(a, s + 1, high,k) ;
else return selection(a, low , s -1 ,k) ;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -