作者markoo (上行下笑阿..)
看板NCTU-STAT98G
標題0721 統算 example 2-1.cpp
時間Tue Jul 21 12:42:17 2009
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i,j,k;
int n; //size of data
int index;
double temp,temp2;
double *data;
printf("Input the size of data:");
scanf("%d",&n);
data = (double *) malloc (n*sizeof(*data));
for (i=0;i<n;i++)
{
data[i] = rand()/32767.0;
printf("%lf\n",data[i]);
}
system("pause");
i=0;
j=n-1;
temp = data[(n-1)/2];
do{
while (data[i] < temp && i < n) i++;
while (temp < data[j] && j > 0) j--;
printf("i=%d, j= %d, temp = %lf\n",i,j,temp);
if ( i<=j )
{
temp2 = data[i];
data[i] = data[j];
data[j] = temp2;
i++;
j--;
}
for (k=0;k<n;k++) printf("%lf\n",data[k]);
system("pause");
}while (i<=j);
/*
計算比temp值大的個數,最後存成i
計算比temp值小的個數,最後存成j
最後從0到j是原本資料中,值較小的一群
從i到n-1則是原本資料中,值較大的一群
以上,是快速排列法的第一步, 接下來嘗試把上列步驟寫成副函式
並把分群後的兩群(小群 and 大群)再重複執行直到排序完成.
*/
return 0;
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.113.7.248