8_74.cpp
上传用户:zipjojo
上传日期:2009-07-20
资源大小:70k
文件大小:1k
- #include<iostream.h>
- #include<iomanip.h>
- template<class T>
- void QuickSort(T*const array,int low,int high)
- { //递归实现
- int i=low,j=high;
- T temp=array[low]; //取第一个对象为进行调整的标准对象
- while(i<j)
- {
- //在数组的右端扫描
- while(i<j && temp<=array[j]) j--;
- if(i<j)
- {
- array[i]=array[j];
- i++;
- }
- //在数组的左端扫描
- while(i<j&& array[i]<temp) i++;
- if(i<j)
- {
- array[j]=array[i];
- j--;
- }
- }
- array[i]=temp;
- //对子对象数组进行递归快速排序
- if(low<i) QuickSort(array,low,i-1);
- if(i<high) QuickSort(array,j+1,high);
- }
- void main()
- {
- const int size=10;
- int a[10]={10,9,8,7,6,5,4,3,2,1},i;
- cout<<"The original order of a:"<<endl;
- for(i=0;i<size;i++)
- cout<<setw(4)<<a[i];
- QuickSort(a,0,size-1);
- cout<<endl<<"The sorted order of a:"<<endl;
- for(i=0;i<size;i++)
- cout<<setw(4)<<a[i];
- cout<<endl;
- char b[10]={'s','T','e','Y','a','A','o','E','c','R'};
- cout<<"The original order of b:"<<endl;
- for(i=0;i<size;i++)
- cout<<setw(4)<<b[i];
- QuickSort(b,0,size-1);
- cout<<endl<<"The sorted order of b:"<<endl;
- for(i=0;i<size;i++)
- cout<<setw(4)<<b[i];
- cout<<endl;
- }