回 帖 发 新 帖 刷新版面

主题:[原创]看一下我编写的快速排序源程序,绝对原创啊!

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h> 

#define N 1000

int array[N+1];

void interchange(int &x,int &y);
int partition(int m,int p);
void quicksort(int p,int q);

void interchange(int &x,int &y)
{
    int elem;
    elem=y;
    y=x;
    x=elem;
}

int partition(int m,int p)
{
    int i,v;
    v=array[m];
    i=m;
    do{
        do{
            i=i+1;
        }while(array[i]<v);

        do{
            p=p-1;
        }while(array[p]>v);

        if(i>=p)  // if ">=" has been change to ">" ,this program is  
            break;
        else interchange(array[i],array[p]);//exit(0);
        }while(1);

    array[m]=array[p];

    array[p]=v;
    return p;
}
void quicksort(int p,int q)
{   
    if(p<q)
    {
        int j;    
        j=q+1;
        j=partition(p,j);

        quicksort(p,j-1);

        quicksort(j+1,q);
    }
}

void main(void)
{
    int n=N;
    int i;
    time_t start,finish;
    ofstream Table;
    for(i=0;i<n;i++)
        array[i]=(int)((rand()/32768.0)*(100000000));
         array[n]=99999999;
    cout<<"排序前:"<<endl;
    for(i=0;i<n;i++)
         {
             cout.fill('0');
             Table.fill('0');
                cout<<setw(8)<<array[i]<<" ";
                Table<<setw(8)<<array[i]<<" ";
           if((i+11)%10==0)
            cout<<endl;
        }

    //cout<<endl;
    time(&start);
     cout<<endl<<"排序前时间为:"<<start<<endl;
    quicksort(0,N-1);
    time(&finish);
    
    Table.open("e:\\result.doc");
    if(!Table.is_open())
              Table<<"open file failed!"<<endl;
    else{
       cout<<"排序后:"<<endl;
         for(i=0;i<n;i++)
         {
             cout.fill('0');
             Table.fill('0');
             cout<<setw(8)<<array[i]<<" ";
             Table<<setw(8)<<array[i]<<" ";
           if((i+11)%10==0)
            cout<<endl;
         }
    cout<<endl<<"排序完毕时间为:"<<finish<<endl;
    cout<<"排序时间为"<<difftime(start,finish)<<endl;
    }
    Table.close();
cout<<endl;

getch();
}

回复列表 (共15个回复)

沙发

没看出有什么特别的!!

板凳

问问,怎么去写程序,是自己写的.好像不知道怎么去想???
数据结构怎么去学呢,我已经学过,好像没学到似的.

3 楼

cout.fill('0');
 Table.fill('0');这两句看不懂呀!

4 楼

我觉得数据结构就是要经常运用。比如说单链表,可以在某些节点类程序中运用。单纯学习图论对于编程没有什么帮助。你可以学习离散数学和运筹中的图论算法,当然,你如果学习过群论,可以与图论结合起来,我现在在搞这项研究,很有用的。

5 楼

其实这个程序只是一个算法书的copy,我只是将这个算法用VC++实现出来。见笑了!

6 楼

cout.fill('0');
一般与setw(n)合用,如setw(8)即要输出8位数字,如果要
输出的数字小于8位,则用0补齐前面空缺位。
 Table.fill('0'); 
前面有定义:ofstream Table;//打开一个文件
想必Table.fill('0')应该懂了吧!

7 楼

so so

8 楼

路过,鼓励一下

9 楼

楼主真是厉害啊

10 楼

int partition(int array[],int n)
{
    int lh,rh,temp,pivot;
    lh=1;
    rh=n-1;
    pivot=array[0];
    while(1)
    {
        if(lh<rh&&array[lh]<pivot) lh++;
        if(lh<rh&&array[rh]>=pivot) rh--;
        if(lh==rh)  break;
        temp=array[lh];
        array[lh]=array[rh];
        array[rh]=temp;
    }
    if(array[lh]>=pivot) return(0);
    array[0]=array[lh];
    array[lh]=pivot;
    return(lh);
}



void SortIntegerArray(int array[],int n)
{
    int boundary;//分割点
    if(n<2) return;
    else
    {
        boundary=partition(array,n);
        SortIntegerArray(array,boundary);
        SortIntegerArray(array+boundary+1,n-boundary-1);
    }
}


void main()
{
    int a[10]={46,56,75,78,45,35,9,24,46,99};
    SortIntegerArray(a,10);
    for(int i=0;i<10;i++)
        printf("%d ",a[i]);
}
    
别人写的快速排序

我来回复

您尚未登录,请登录后再回复。点此登录或注册