快速排序程序(windows vs)
来源:互联网 发布:微软office for mac 编辑:程序博客网 时间:2024/06/08 13:48
// Sort_and_Count.cpp : 定义控制台应用程序的入口点。//#pragma once#include "stdafx.h"#include <fstream>#include <vector>#include <iostream>#include <sstream>using namespace std;int fileLength = 100000;int numbers[100000];//存储所有数据的数组void readData(char* filePath){ifstream fin;string line;int oneNumber;stringstream ss;fin.open(filePath);cout<<"开始读文件..."<<endl;int i=0;while(getline(fin,line)){ss<<line;ss>>oneNumber;ss.clear();numbers[i++] = oneNumber;//cout<<"i="<<i<<endl;}cout<<"读文件结束,i="<<i<<endl;} void printArray(){for(int i=0;i<fileLength;i++){cout<<numbers[i]<<",";}cout<<endl;}void quickSort(int arrayBegin,int arrayEnd){if(arrayBegin>=arrayEnd){return;}//cout<<arrayBegin<<","<<arrayEnd<<endl;int leftIndex = arrayBegin;int rightIndex = arrayEnd;int center = numbers[leftIndex];//选定最左端的元素作为中轴值int centerIndex = leftIndex;//中轴值的indexwhile(leftIndex<=rightIndex){//从后向前找,找到比中轴值小的元素,写到中轴位置处while(leftIndex<=rightIndex&&numbers[rightIndex]>=center){rightIndex--;}if(leftIndex<=rightIndex){//找到了numbers[centerIndex]=numbers[rightIndex];centerIndex = rightIndex;rightIndex--;}//从前向后找,找到比中轴值大的元素,写到中轴值位置处while(leftIndex<=rightIndex&&numbers[leftIndex]<=center){leftIndex++;}if(leftIndex<=rightIndex){//找到了numbers[centerIndex] = numbers[leftIndex];centerIndex=leftIndex;leftIndex++;}}numbers[centerIndex]=center;quickSort(arrayBegin,centerIndex-1);quickSort(centerIndex+1,arrayEnd);}void writeData(char * filePath){ofstream fout;stringstream ss;string s;fout.open(filePath);cout<<"开始写文件..."<<endl;for(int i=0;i<fileLength;i++){ss<<numbers[i];ss>>s;ss.clear();fout<<s<<endl;}cout<<"写文件结束"<<endl;}int _tmain(int argc, _TCHAR* argv[]){readData("Q5.txt");cout<<"开始快排..."<<endl;quickSort(0,fileLength-1);cout<<"结束快排..."<<endl;writeData("result.txt");return 0;}
0 0
- 快速排序程序(windows vs)
- 快速排序VS堆排序
- 快速排序Quicksort(附快速排序程序)
- (转载)利用vs.net快速开发windows服务(c#)
- 快速排序算法程序
- 快速排序程序
- 快速排序法程序
- 一个快速排序程序
- 快速排序程序
- 一个快速排序程序
- 快速排序pascal程序
- 4.F# vs C#: 快速排序
- 利用VS.NET快速开发WINDOWS服务
- C语言程序----排序(直接插入排序,SHELL排序,冒泡排序,快速排序,简单选择排序,堆排序)
- .Net Windows服务(一):VS快速创建一个简单Windows服务
- 快速排序的递归程序
- C# 小程序 快速排序
- c语言 程序 快速排序
- RGB与YUV格式简介
- 民院CSDN俱乐部首次会员培训活动顺利开展
- 目前交了但未AC的题目(及时更新)
- (二)线程同步_1---同步一个方法
- 《java语言程序设计》习题5.25
- 快速排序程序(windows vs)
- 安卓之旅(2)--添加一个ActionBar (1)
- SQLCode
- HDURevenge of Segment Tree(第二长的递增子序列)
- java方法重载
- C#设计模式之中介者模式(Mediator Pattern)
- 整理
- 11月风怒
- HDU—— 4956 Poor Hanamichi