插入,冒泡,快速排序

// lb1.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
/*
c1.h (程序名) */
#include<string.h>
#include<iostream>

#include<ctype.h>
#include<malloc.h> /* malloc()等 */
#include<limits.h> /* INT_MAX等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<string>
using namespace std;
int Part(int *a[20],int low,int high);
int n;

void a()
{
int a[20];
cout<<"输入需要排序的整数的个数(20个以内):";
cin>>n;
cout<<"请输入"<<n<<"个以内的整数";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int k,st;
for(int j=1;j<n;j++)
{
st=a[j];
k=j;
while(k>0&&a[k-1]>=st)
{
a[k]=a[k-1];
k--;
}
a[k]=st;
for(int l=0;l<n;l++)
{
cout<<a[l]<<" ";
}
cout<<endl;
}
}

void b()
{
int a[20];
cout<<"输入需要排序的整数的个数(20个以内):";
cin>>n;
cout<<"请输入"<<n<<"个以内的整数";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
int st;
for(int k=n;k>0;k--)
{
for(int j=0;j<k-1;j++)
{
if(a[j]>=a[j+1])
{
st=a[j];
a[j]=a[j+1];
a[j+1]=st;
}
}
for(int l=0;l<n;l++)
{
cout<<a[l]<<" ";
}
cout<<endl;
}
}

void rc(int *a[20],int low, int high)
{
if(high>low)
{
int mid=Part(a,low,high);
for(int l=0;l<n;l++)
{
cout<<*a[l]<<" ";
}
cout<<endl;
rc(a,low,mid-1);
rc(a,mid+1,high);
}
}

int Part(int *a[20],int low,int high)
{
int Var=*a[low];
int prvotkey=low;
while(low<high)
{
while(low<high && *a[high]>=Var) --high;
*a[low]=*a[high];
while(low<high && *a[low]<=Var) ++low;
*a[high]=*a[low];
}
*a[low]=Var;
return low;
}

void c()
{
int a[20];
int *p[20];
cout<<"输入需要排序的整数的个数(20个以内):";
cin>>n;
cout<<"请输入"<<n<<"个以内的整数";
for(int i=0;i<n;i++)
{
cin>>a[i];
p[i] = &a[i];
}
rc(p,0,n-1);
}



int main(int argc, char* argv[])
{
int choose;
do
{
cout<<"1-插入排序"<<endl;
cout<<"2-冒泡排序"<<endl;
cout<<"3-快速排序"<<endl;
cout<<"0-退出"<<endl;
cout<<"选择:";
cin>>choose;
switch(choose)
{
case 1:
a();
break;
case 2:
b();
break;
case 3:
c();
break;
case 0:
cout<<"运行结束!";
break;
default:
cout<<"输入错误重新选择:";
break;
}
}while(choose!=0);
return 0;
}

 

你可能感兴趣的:(快速排序)