16,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
typedef struct {
int L[10];
int length;
}List,*list;
void swap(List &L,int low,int high){
int temp;
temp=L.L[low];
L.L[low] = L.L[high];
L.L[high]=temp;
}
/**************************************
* 快速排序 *
* ************************************/
int Partition(List &L,int low,int high){
int pro;
pro=L.L[low];
while(low<high){
while(L.L[high]>=pro&&low<high){
high--;
}
swap(L,low,high);
while(L.L[low]<=pro&&low<high){
low++;
}
swap(L,low,high);
}
//L.L[low]=L.L[0];
return low;
}
void Qsort(List &list,int low,int high){
int pivot;
if(low<high){
pivot = Partition(list,low,high);
Qsort(list,low,pivot-1);
Qsort(list,pivot+1,high);
}
}
/****************************
* 冒泡排序 *
* **************************/
void bubbleSort(List &list){
int i,j;
bool flag=1;
for(i=1;i<=list.length&&flag;i++){
flag=0;
for(j=list.length-1;j>=i;j--){
if(list.L[j]>list.L[j+1]){
swap(list,j,j+1);
flag=1;
}
}
}
}
/***************************
* 简单选择排序 *
***************************/
void selectSort(List &list){
int i,j,min;
for(i=0;i<=list.length;++i){
min=i;
for(j=i+1;j<=list.length;++j){
if(list.L[min]>list.L[j]){
//swap(list,min,j);
min=j;
}
}
if(i!=min){
swap(list,min,i);
}
}
}
/*************************
* 直接插入排序 *
*************************/
void insertSort(List &list){
int i,j;
for(i=2;i<=list.length;++i){
if(list.L[i]<list.L[i-1]){
list.L[0]=list.L[i];
for(j=i-1;list.L[j]>list.L[0];--j){
list.L[j+1]=list.L[j];
}
list.L[j+1]=list.L[0];
}
}
}
/***********************
* 希尔排序 *
***********************/
void sheelSort(List &list){
int i,j;
int increment=list.length;
do{
increment = increment/3+1;
for(i=increment+1;i<=list.length;++i){
if(list.L[i]<list.L[i-increment]){
list.L[0]=list.L[i];
for(j=i-increment;list.L[0]<list.L[j]&&j>0;j-=increment){
list.L[j+increment]=list.L[j];
}
list.L[j+increment] = list.L[0];
}
}
}while(increment!=1);
}
int main(){
List list={{7,3,2,6,7,8,9,0,9,2},9};
//Qsort(list,0,9); //快速排序
//bubbleSort(list);
//selectSort(list);
//insertSort(list);
sheelSort(list);
int i=0;
for(i=0;i<10;++i){
std::cout<<list.L[i]<<" ";
}
std::cout<<std::endl;
return 0;
}
#include <QCoreApplication>
#include "tinyxml.h"
#include "tinystr.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TiXmlDocument *pDoc=new TiXmlDocument;
pDoc->SaveFile("example.xml");
return a.exec();
}