排序算法演示程序

来源:互联网 发布:嘉兴日报网络订报 编辑:程序博客网 时间:2024/05/01 02:33
 

演示了顺序和链式的几种排序算法

相关算法及程序如下:

1、main.cpp

#include "head.h"
void main() { char ch; SetColor(); //调用API函数,实现控制台背景色改变。 introduction();
instructions();

lable1:cout<<"系统将生成"<<geshu<<"个随机数,请您选择它们的存储结构:/n"   <<"1.用顺序结构存储/n"   <<"2.用链式结构存储/n"<<endl;   cin>>ch; switch(ch) { case '1':clistc();break; case '2':llistc();break; default:cout<<"输入错误,请重新输入。/n";goto lable1;break; } cout<<"是否继续?(y/n)/n"; cin>>ch; switch(ch) {  case 'y':main();  default:exit (0); } }

2、clist.h(顺序表实现)

#ifndef _cList_H//顺序表实现 #define _cList_H const int max_list=10000; enum cError_code{range_errorc,successc,failc,underflowc,overflowc}; template <class List_entry> class cList{  protected:   int count;   List_entry entry[max_list];   List_entry d[max_list];  public:   cList();   int size()const;   bool full()const;   bool empty()const;   void clear();   void print();   void traverse(void (*vist)(List_entry &));   cError_code retrieve(int position,List_entry &x);   cError_code replace(int position,const List_entry &x);   cError_code remove(int position,List_entry &x);   cError_code insert(int position,const List_entry &x); }; template <class List_entry> cList<List_entry>::cList() {  count=0; } template <class List_entry> int cList<List_entry>::size()const {  return count; } template <class List_entry> bool cList<List_entry>::full()const {  return count==max_list; } template <class List_entry> bool cList<List_entry>::empty()const {  return count==0; } template <class List_entry> void cList<List_entry>::clear() {  count=0; } template <class List_entry> void cList<List_entry>::traverse(void (*visit)(List_entry &)) {  for (int i=0;i<count;i++)   (*visit)(entry[i]); } template <class List_entry> cError_code cList<List_entry>::retrieve(int position,List_entry &x) {  if (position<0)   return underflowc;  else  {   x=entry[position];   return successc;  } } template <class List_entry> cError_code cList<List_entry>::replace(int position,const List_entry &x) {  if (position<0)   return underflowc;  else  {   entry[position]=x;   return successc;  } } template <class List_entry> cError_code cList<List_entry>::remove(int position,List_entry &x) {  if (position<0)   return underflowc;  else  {    x=entry[position];    count--;       return successc;  } } template <class List_entry> cError_code cList<List_entry>::insert(int position,const List_entry &x) {  if (full())   return overflowc;  if (position<0||position>count)   return overflowc;  for (int i=count-1;i>=position;i--)   entry[i+1]=entry[i];  entry[position]=x;  count++;  return successc; } template <class List_entry> void cList<List_entry>::print() {     int x;  for (int i=0;i<size();i++)  {   retrieve(i,x);   if (i%5==0)cout<<endl;   cout<<setw(7)<<x<<"  ";  }  cout<<endl; } #endif

3、llist.h(链式线性表实现)

#ifndef _lList_H #define _lList_H//链表实现 enum lError_code{range_errorl,successl,faill,underflowl,overflowl}; //节点定义 template <class Node_entry> struct Node{ Node_entry entry; Node<Node_entry> *next; Node(); Node(Node_entry item,Node<Node_entry> *link=NULL); }; template <class Node_entry> Node<Node_entry>::Node() {next=NULL;} template <class Node_entry> Node<Node_entry>::Node(Node_entry item,Node<Node_entry> *link) {  entry=item;  next=link; } //链表定义 template <class List_entry> class lList{  protected:   int count;   Node<List_entry> *head;   void set_position(int position)const;   mutable int current_position;   mutable Node<List_entry> *current;  public:   lList();   ~lList();   lList(const lList<List_entry> &copy);   void operator=(const lList<List_entry> &copy);   int size()const;   bool empty()const;   void clear();   void print();   void traverse(void (*vist)(List_entry &));   lError_code retrieve(int position,List_entry &x);   lError_code replace(int position,const List_entry &x);   lError_code remove(int position,List_entry &x);   lError_code insert(int position,const List_entry &x); }; template <class List_entry> void lList<List_entry>::set_position(int position)const//辅助函数,确定要插入位置的指针 {  if (position<=current_position){   current_position=0;   current=head;  }  for (;current_position!=position;current_position++)   current=current->next; } template <class List_entry>//构造函数 lList<List_entry>::lList() {  head=NULL;  current_position=0;  current=head;  count=0; } template <class List_entry>//析构函数 lList<List_entry>::~lList() {  List_entry temp;  while (!empty())   remove(--count,temp); } template <class List_entry>//拷贝构造函数 lList<List_entry>::lList(const lList<List_entry> &copy) {  Node *new_copy,*original_node=copy.head;  if (original_node==NULL)head=NULL;  else {   head=new_copy=new Node<List_entry>(original_node->entry);   while (original_node->next!=NULL){    original_node=original_node->next;    new_copy->next=new Node(original_node->entry);    new_copy=new_copy->next;   }  } } template <class List_entry> void lList<List_entry>::operator=(const lList<List_entry> &copy)//=重载 {  Node *new_copy,*original_node=copy.head;  if (original_node==NULL)head=NULL;  else {   head=new_copy=new Node<List_entry>(original_node->entry);   while (original_node->next!=NULL){    original_node=original_node->next;    new_copy->next=new Node(original_node->entry);    new_copy=new_copy->next;   }  } } template <class List_entry> lError_code lList<List_entry>::insert(int position,const List_entry &x) {  if (position<0||position>count)   return range_errorl;  Node<List_entry> *new_node,*previous,*following;  if (position>0){   set_position(position-1);   previous=current;   following=current->next;  }//设置前后节点指针  else following=head;//等于○时后继节点指针  new_node=new Node<List_entry>(x,following);//分配空间  if (new_node==NULL)   return overflowl;//如果空间分配失败,则溢出  if (position==0)   head=new_node;//如果是往头节点插入  else   previous->next=new_node;//不是往头节点插入  count++;//计数自加  return successl; } template <class List_entry> int lList<List_entry>::size()const {  return count; } template <class List_entry> bool lList<List_entry>::empty()const {  return count==0; } template <class List_entry> void lList<List_entry>::clear() {  List_entry temp;  while (!empty())   remove(--count;temp); } template <class List_entry> void lList<List_entry>::traverse(void (*visit)(List_entry &)) {  Node *old_head=head;  while (old_head!=NULL)  {(*visit)(old_head->entry);old_head=old_head->next;} } template <class List_entry> void lList<List_entry>::print() {     int x;    for (int i=0;i<size();i++)  {   retrieve(i,x);   if (i%5==0)cout<<endl;   cout<<setw(7)<<x<<"  ";  }  cout<<endl; } template <class List_entry> lError_code lList<List_entry>::retrieve(int position,List_entry &x) {  if (position<0||position>count)   return range_errorl;  Node<List_entry> *pos;  set_position(position);  pos=current;  x=pos->entry;  return successl; } template <class List_entry> lError_code lList<List_entry>::replace(int position,const List_entry &x) {  if (position<0||position>count)   return range_errorl;  Node<List_entry> *pos;  set_position(position);  pos=current;  pos->entry=x;  return successl; } template <class List_entry> lError_code lList<List_entry>::remove(int position,List_entry &x) {  if (position<0||position>count)   return range_errorl;  Node<List_entry> *previous,*pos;  if (position>0){   set_position(position-1);   previous=current;   set_position(position);   pos=current;   previous->next=pos->next;   delete pos;   return successl;  }  if (position==0){   set_position(position);   pos=current;   head=pos->next;   delete pos;   return successl;  }  else return faill; } #endif

4、fun.h(相关函数定义)

int geshu;//记录随机数个数 void SetColor(unsigned short ForeColor=4,unsigned short BackGroundColor=0) { HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hCon,FOREGROUND_GREEN); }; //利用API函数来改变控制台程序的字体颜色,每次都是白色的太单调了。有关该函数的相关说明请查阅MSDN. void introduction() {  cout<<"排序算法应用器[版本:1.0.0623]"<<endl;  cout<<"(C) 版权所有 2005-2007 Lthsoft Corp. 作者:usufu "<<endl<<endl; } void instructions() {  cout<<"系统会随机生成随机数,然后您可以选择这些数字的排序算法,进行排序"   <<"请选择要生成随机数的个数:"<<endl;  cin>>geshu; } int rannum(int n)//生成随机数 {  srand (n*13*time(0));//用time函数来获得系统当前时间,使得每次运行取得的随机数都不同.  return rand();//乘以一个因子13,使得每次取得的数差别大些. } void clistc()//顺序表存储排序 {  if (geshu>max_list||geshu<=1)  {cout<<"超过顺序表的最大长度或者小于等于1,请重新输入/n";  instructions();}  cSortable_list<int> list;  char ch;  for (int i=0;i<geshu;i++)   list.insert(i,rannum(i+1));  cout<<"以下为系统生成的将要排序的随机数:/n";  list.print(); lop1: cout<<"请选择排序算法:/n"   <<"1.插入排序/n"   <<"2.选择排序/n"   <<"3.希尔排序/n"   <<"4.快速排序/n"   <<"5.堆排序/n"   <<"6.归并排序/n"   <<endl;  cin>>ch;  switch(ch)  {  case '1':list.insertion_sort();break;  case '2':list.selection_sort();break;  case '3':list.shell_sort();break;  case '4':list.quick_sort();break;  case '5':list.heap_sort();break;  case '6':list.merge_sort();break;  default:cout<<"输入错误,请重新输入/n";goto lop1;break;  }  list.print(); } void llistc()//链式存储排序 {  lSortable_list<int> list;  char ch;  for (int i=0;i<geshu;i++)   list.insert(i,rannum(i+1));  cout<<"以下为系统生成的将要排序的随机数:/n";  list.print(); lop1:cout<<"请选择排序算法:/n"   <<"1.插入排序/n"   <<"2.归并排序/n"   <<endl;  cin>>ch;  switch(ch)  {  case '1':list.insertion_sort();break;  case '2':list.merge_sort();break;  default:cout<<"输入错误,请重新输入/n";goto lop1;break;  }  list.print(); }

5、head。h(本程序包含的所有头文件)

#include <iostream>//本程序所需要的所有头文件 #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; #include <windows.h>//包含API函数原型 #include "clist.h" #include "lList.h" #include "sort.h" #include "fun.h"

6、sort。h(顺序和链式排序派生类)

template <class Record>//排序派生类(顺序) class cSortable_list:public cList<Record> {  public:   void insertion_sort();   void selection_sort();   void shell_sort();   void quick_sort();   void heap_sort();   void merge_sort();  private:   int max_key(int low,int high);   void swap(int low,int high);   void sort_interval(int start,int increment);   void recursive_quick_sort(int low,int high);   int partition(int low,int high);   void insert_heap(const Record &current,int low,int high);   void build_heap();   void recursive_merge_sort(int left,int right);   void merge(int left,int mid,int right);   void copy(int left,int right); }; //插入法排序 template <class Record> void cSortable_list<Record>::insertion_sort() {  int first_unsorted;  int position;  Record current;  for (first_unsorted=1;first_unsorted<count;first_unsorted++)   if (entry[first_unsorted]<entry[first_unsorted-1])   {    position=first_unsorted;    current=entry[first_unsorted];    do    {     entry[position]=entry[position-1];     position--;    }while (position>0&&entry[position-1]>current);    entry[position]=current;   } } //选择法排序 template <class Record> void cSortable_list<Record>::selection_sort() {  for (int position=count-1;position>0;position--){  int max=max_key(0,position);  swap(max,position);  } } template <class Record> int cSortable_list<Record>::max_key(int low,int high) {  int largest,current;  largest=low;  for (current=low+1;current<=high;current++)   if (entry[largest]<entry[current])    largest=current;   return largest; } template <class Record> void cSortable_list<Record>::swap(int low,int high) {  Record temp;  temp=entry[low];  entry[low]=entry[high];  entry[high]=temp; } //希尔排序 template <class Record> void cSortable_list<Record>::shell_sort() {  int increment,start;  increment=count;  do {   increment=increment/3+1;   for (start=0;start<increment;start++)    sort_interval(start,increment);  }while (increment>1); } template <class Record> void cSortable_list<Record>::sort_interval(int start,int increment) {  int first_unsorted;  int position;  Record current;  for (first_unsorted=start+1;first_unsorted<count;first_unsorted+=increment)   if (entry[first_unsorted]<entry[first_unsorted-1])   {    position=first_unsorted;    current=entry[first_unsorted];    do    {     entry[position]=entry[position-1];     position--;    }while (position>0&&entry[position-1]>current);    entry[position]=current;   } } //快速排序 template <class Record> void cSortable_list<Record>::quick_sort() {  recursive_quick_sort(0,count-1); } template <class Record> void cSortable_list<Record>::recursive_quick_sort(int low,int high) {  int pivot_position;  if (low<high){  pivot_position=partition(low,high);  recursive_quick_sort(low,pivot_position-1);  recursive_quick_sort(pivot_position+1,high);  } } template <class Record> int cSortable_list<Record>::partition(int low,int high) {  Record pivot;  int i,last_small;  swap(low,(low+high)/2);  pivot=entry[low];  last_small=low;  for (i=low+1;i<=high;i++)   if (entry[i]<pivot){   last_small=last_small+1;   swap(last_small,i);   }   swap(low,last_small);   return last_small; } //堆排序 template <class Record> void cSortable_list<Record>::heap_sort() {  Record current;  int last_unsorted;  build_heap();  for (last_unsorted=count-1;last_unsorted>0;last_unsorted--){  current=entry[last_unsorted];  entry[last_unsorted]=entry[0];  insert_heap(current,0,last_unsorted-1);  }

} template <class Record> void cSortable_list<Record>::insert_heap(const Record &current,int low,int high) {  int large;  large=2*low+1;  while (large<=high){  if (large<high&&entry[large]<entry[large+1])   large++;  if (current>=entry[large])   break;  else {   entry[low]=entry[large];   low=large;   large=2*low+1;   }  }  entry[low]=current; } template <class Record> void cSortable_list<Record>::build_heap() {  int low;  for (low=count/2-1;low>=0;low--){  Record current=entry[low];  insert_heap(current,low,count-1);  } } //归并排序 template <class Record> void cSortable_list<Record>::merge_sort() {  recursive_merge_sort(0,count-1); } template <class Record> void cSortable_list<Record>::recursive_merge_sort(int left,int right) {  if (left<right){//至少两个元素  int i=(left+right)/2;//中心位置  recursive_merge_sort(left,i);  recursive_merge_sort(i+1,right);  merge(left,i,right);//合并到备用数组d中  copy(left,right);//合并回原始数组entry中  } } template <class Record> void cSortable_list<Record>::merge(int left,int mid,int right) {  int i=left,//第一段的游标   j=mid+1,//第二段的游标   k=left;//结果的游标  while((i<=mid)&&(j<=right))//只要在段中存在i和j,则不断进行归并   if (entry[i]<=entry[j])    d[k++]=entry[i++];   else d[k++]=entry[j++];  if (i>mid)//考虑余下的部分   for (int q=j;q<=right;q++)    {d[k++]=entry[q];}  else   for (int q=i;q<=mid;q++)    {d[k++]=entry[q];} } template <class Record>//将备用数组中的数字,合并到原始数组中 void cSortable_list<Record>::copy(int left,int right) {  for (int i=left;i<=right;i++)  entry[i]=d[i]; }

template <class Record>//排序派生类(链式) class lSortable_list:public lList<Record> {  public:   void insertion_sort();   void merge_sort();  private:   void recursive_merge_sort(Node<Record> *&sub_list);   Node<Record> *divide_from(Node<Record> *sub_list);   Node<Record> *merge(Node<Record> *first,Node<Record> *second); }; template <class Record> void lSortable_list<Record>::insertion_sort() {  Node<Record> *first_unsorted,      *last_sorted,      *lcurrent,      *trailing;  if (head!=NULL){   last_sorted=head;   while (last_sorted->next!=NULL){    first_unsorted=last_sorted->next;    if (first_unsorted->entry<head->entry){//和头进行比较     last_sorted->next=first_unsorted->next;     first_unsorted->next=head;     head=first_unsorted;    }    else {//和头以后的元素进行比较     trailing=head;     lcurrent=trailing->next;     while (first_unsorted->entry>lcurrent->entry){      trailing=lcurrent;                     lcurrent=trailing->next;     }     if (first_unsorted==lcurrent)      last_sorted=first_unsorted;     else {      last_sorted->next=first_unsorted->next;      first_unsorted->next=lcurrent;      trailing->next=first_unsorted;     }    }   }  } } template <class Record> void lSortable_list<Record>::merge_sort() {  recursive_merge_sort(head); } template <class Record> void lSortable_list<Record>::recursive_merge_sort(Node<Record> *&sub_list) {  if (sub_list!=NULL&&sub_list->next!=NULL){   Node<Record> *second_half=divide_from(sub_list);   recursive_merge_sort(sub_list);   recursive_merge_sort(second_half);   sub_list=merge(sub_list,second_half);  } } template <class Record> Node<Record> *lSortable_list<Record>::divide_from(Node<Record> *sub_list) {  Node<Record> *position,      *midpoint,      *second_half;  if ((midpoint=sub_list)==NULL)return NULL;  position=midpoint->next;  while (position!=NULL){   position=position->next;   if (position!=NULL){    midpoint=midpoint->next;    position=position->next;   }  }  second_half=midpoint->next;  midpoint->next=NULL;  return second_half; } template <class Record> Node<Record> *lSortable_list<Record>::merge(Node<Record> *first,Node<Record> *second) {  Node<Record> *last_sorted;  Node<Record>combined;  last_sorted=&combined;  while (first!=NULL&&second!=NULL){   if (first->entry<=second->entry){    last_sorted->next=first;    last_sorted=first;    first=first->next;   }   else {    last_sorted->next=second;    last_sorted=second;    second=second->next;   }  }  if (first==NULL)   last_sorted->next=second;  else   last_sorted->next=first;  return combined.next; }

原创粉丝点击