Comparative analysis of 10 kinds of internal sorting algorithms in C + + _c language

Source: Internet
Author: User
Tags clear screen int size

Comparative analysis of 10 kinds of internal sorting algorithms in C + +

#include <iostream> #include <ctime> #include <fstream> using namespace std;  #define The maximum length of the MAXSIZE 1000//sortable table #define SORTNUM 10//test 10 The ranking method #define MAX 100//cardinality when the maximum number of digits is not more than hundred; typedef struct
  node {int data3;
int next;
node;
typedef int DATATYPE[MAXSIZE+2];
DataType data;
DataType data2;
DataType R1;
int size;//The length of the sortable table int head;
int fr[10];
int re[10];
   Long compcount;//statistic comparison number long shiftcount;//statistic move number void Beforesort ()/To compare times and move number of clear 0 {compcount=0;
 shiftcount=0;
   BOOL Less (int i,int j)//If the first element in the table is less than the J element, returns True, otherwise returns false {compcount++;
 Return data[i]<data[j];
   } void Swap (int i,int j)//interchange table with the first and J elements {int A;
   A=data[i];
   DATA[I]=DATA[J];
   Data[j]=a;
 shiftcount=shiftcount+3;
   } void Shift (DataType &r,datatype &r2,int i,int J)//assigns r2[j] to R[i] {r[i]=r2[j];
 shiftcount++;
   } void CopyData (DataType list1,datatype list2) {int i;
   
 for (i=1;i<=size;i++) list2[i]=list1[i]; } void InverseordER ()//To place the sortable table in reverse order {int i,j;
     for (i=1,j=size;i<=size/2;i++,j--) {int A;
     A=data[i];
     DATA[I]=DATA[J];
   Data[j]=a;
 } copydata (DATA,DATA2);
   } void Randomizelist ()//by system random set of number {int i;
   Srand (Time (0));
   for (i=1;i<=size;i++) Data[i]=rand ()% (size+1); 
   CopyData (DATA,DATA2);
   Ofstream Out_stream;
   Out_stream.open ("Input.txt", Ios::app);
     if (Out_stream.fail ()) {cout<< "input file opening failed.\n";
   Exit (1);
   for (i=1;i<=size;i++) out_stream<<data[i]<< "";
   out_stream<< "\ n"; 
 Out_stream.close ();
 void Recalllist ()//Recover the last sortable randomizelist randomly scrambled table {copydata (data2,data);
   A void output ()/output function {Ofstream out_stream;
   cout<< "T" <<compCount<< "\t\t" <<shiftCount<< "\ n";
   Out_stream.open ("Output.txt", Ios::app);
     if (Out_stream.fail ()) {cout<< "Output file opening failed.\n";
   Exit (1); } out_stream<< "\ t" <<compcount<< "\t\t" <<shiftCount<< "\ n";   
 Out_stream.close ();
   } void Bubblesort ()//bubble Sort {beforesort ();
   int swapped,i,m;
   m=size-1;
     do{swapped=0;
         for (i=1;i<=m;i++) {if (less (i+1,i)) {Swap (i+1,i);
       swapped=1;
   }} m--;
   }while (swapped);
 Output ();
   } void Insertsort ()//insert Sort {beforesort ();
   int i,j;
     for (i=2;i<=size;i++) {Shift (data,data,0,i);
     J=i-1;
       while (less (0,j)) {Shift (data,data,j+1,j);
     j--;
   Shift (data,data,j+1,0);
 Output ();
   } void Selectsort ()//select sort {beforesort ();
   int i,j,min;
     for (i=1;i<=size-1;i++) {min=i;
     for (j=i+1;j<=size;j++) if (less (j,min)) min=j;
   if (i!=min) Swap (i,min);
 Output ();
   int Partition (int low,int high) {int pivotkey;
   Shift (Data,data,0,low);
   Pivotkey=data[low];
     while (Low 

The above is the entire content described in this article, I hope to be familiar with the master of these 10 sorting algorithms help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.