数据结构之(快速排序)代码

来源:互联网 发布:幕府将军2优化补丁 编辑:程序博客网 时间:2024/06/18 10:22
// sort.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>using namespace std;void swap_var(float l[],int low,int high);int Partition(float l[],int low, int high);void QSort(float l[],int low,int high);void QuickSort(float l[],int t);int _tmain(int argc, _TCHAR* argv[]){float* array=new float[10];for (int i=0;i<10;i++){array[i]=(10-i);}cout<<"排序前的数组是:"<<endl;for (int i=0;i<10;i++){cout<<array[i]<<"  ";}cout<<endl;cout<<"排序后的数组是:"<<endl;QuickSort(array,10);for (int i=0;i<10;i++){cout<<array[i]<<"  ";}cout<<endl;delete[] array;return 0;}void QuickSort(float l[],int t){//int z=sizeof(l)/sizeof(float);//数组的长度QSort(l,0,t-1);}void QSort(float l[],int low,int high){int pivot;if (low<high){pivot=Partition(l,low,high);if(pivot>0)QSort(l,low,pivot-1);if(pivot<high)QSort(l,pivot+1,high);}}int Partition(float l[],int low, int high){int pivotkey;pivotkey=l[low];//这里pivotkey也可以用三中取一法或者九中取一法while(low<high){while(low<high&&((l[high]>pivotkey)||(l[high]==pivotkey)))high--;swap_var(l,low,high);while(low<high&&((l[low]<pivotkey)||(l[low]==pivotkey)))low++;swap_var(l,low,high);}return low;}void swap_var(float l[],int low,int high){float temp;temp=l[low];l[low]=l[high];l[high]=temp;}


原创粉丝点击