插入,冒泡,快速排序

// 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;
}

 

转载于:https://www.cnblogs.com/suguoqiang/archive/2012/02/26/2369160.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值