作者ding@kkcity.com.tw (人生如浮雲), 信區: sources
標題Re: [問題] 如何用亂數方法寫快速排序(quick sort)???
時間KKCITY (Mon Dec 13 01:22:43 2004)
轉信站: GIBBS!news2.ncku!ccnews.ncku!news.ccns.ncku!news.mksh.phc!news.nsysu!Sp
Origin: bbs.kkcity.com.tw
※ 引述《pinkid.bbs@venus.nhust.edu.tw (一顆心屬於一個人)》之銘言:
> 各位bb大大...
> 請問有誰會用java的方式...
> 寫快速排序法(quick sort)??
> 而且數值是要用亂數的方法喔...
> 懇求大家幫忙...0.0
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int a[30000], k, j;
float t;
int partition( int a[], int low, int high )
{
int left, right;
int pivot_item;
int rand;
rand=low+random(high-low+1);
pivot_item=a[rand];
a[rand]=a[low];
a[low]=pivot_item;
left = low;
right = high;
while ( left < right ) {
/* Move left while item < pivot */
while( a[left] <= pivot_item ) left++;
/* Move right while item > pivot */
while( a[right] > pivot_item ) right--;
if ( left < right )
{int temp; temp=a[left];a[left]=a[right];a[right]=temp;};
};
/* right is final position for the pivot */
a[low] = a[right];
a[right] = pivot_item;
return right;
};
void quicksort( int a[], int low, int high )
{
int pivot;
/* Termination condition! */
if ( high > low )
{
pivot = partition( a, low, high );
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high );
}
}
void main()
{
clrscr();
for (k = 0;k <= 29999; k++)
{
a[k] = random(30000)
;
}
quicksort(a,0,29999);
for (k = 0;k <= 29999; k++)
{
if(k%10==0)
printf("\n");
printf ("%d,",a[k]);
}
getch();
}
--
┌─────◆KKCITY◆─────┐ KK免/費/撥/接 ◤
│ bbs.kkcity.com.tw │▏電話(1):449◤1999 電話(2):4058-6000
└──《From:218.166.116.252 》──┘▏帳號:kkcity 密碼:kkcity