Experiment Report--Direct insert sort, improved bubble sort and merge sort implement sorting of integer array

Source: Internet
Author: User

Experiment Six: SortOriginal page Link: Your current location: Home > Course Experiment

Experimental project name: sort

experimental purposes and requirements:1. Familiar with the characteristics of commonly used sorting methods. 2. Apply sorting methods to solve specific problems. experimental Principles and illustrations: 1. The sequence (13,6,3,31,9,27,5,11) is inserted directly into the ordering process as follows:
Initial: "13", 6, 3, 31, 9, 27, 5, 11
1th trip: "6, 13", 3, 31, 9, 27, 5, 11
2nd trip: "3, 6, 13", 31, 9, 27, 5, 11
3rd trip: "3, 6, 13,31", 9, 27, 5, 11
4th Trip: "3, 6, 9, 13,31", 27, 5, 11
5th trip: "3, 6, 9, 13, 27, 31", 5, 11
6th trip: "3, 5, 6, 9, 13, 27, 31", 11
7th trip: "3, 5, 6, 9, 11,13,27, 31"

2. The specific implementation of sequence t= (21,25,49,25*,16,08) bubble sequencing is as follows:
Initial: 21,25,49, 25*,16,08
1th trip: 08,21,25, 49, 25*,16
2nd trip: 08,16, 21, 25, 49, 25*
3rd trip: 08,16, 21, 25, 25*, 49
4th trip: 08,16, 21, 25, 25*, 49--no swap, end
5th trip: 3. The specific implementation of sequence t= (49,38,65,97,76,13,27) 2-way merge sort is as follows:
The basic description of the experiment:

Write code to sort the integers in a direct insert sort, an improved bubbling sort, and a merge sort, and analyze the complexity of the time.

Experimental steps:
1. The sorting of integer arrays is realized by direct inserting sort, improved bubble sort and merge sort, and the complexity of time is analyzed.
2. Call execution in the main function, input and record the output result validation;
3. Debug, record the problems and the results of the final operation;
4. Submit the electronic work and fill in the experimental report. —————————————————————————————————————————————— do not need any input, the code can display the process of sorting, the third kind of 2-way merge sort does not show the sorting process (seems not to AH)!
#include <iostream>#include<stdio.h>#include<math.h>using namespacestd;intstep0=0;//2 Way-merge sort record Step numbervoidPRINT_ARR1 (intNintA[],intStep//Direct Insert sort, output array a{    if(step==0) printf ("initial: ""); Elseprintf ("trip%d: "", step);  for(intI=1; i<=n-1; i++) {printf ("%2d", A[i]); if(i==step+1) printf ("","); Elseprintf","); }     if(step+1==N) printf ("%d "\ n", A[n]); Elseprintf"%d\n", A[n]);}voidPRINT_ARR2 (intNintA[],intStepintK//bubble sort, output array b{    if(step==0) printf ("initial:"); Elseprintf ("Trip%d:", step);  for(intI=1; i<=n-1; i++) {printf ("%02d", A[i]); if(i==step+1) printf (","); Elseprintf","); }     if(k==0) printf ("%02d--no swap, end \ n 5th trip: \ n", A[n]); Elseprintf"%02d\n", A[n]);}voidPRINT_ARR3 (intNintA[])//2-way merge sort, output array C{    if(step0==0) {printf ("Initial Keywords:"); step0=1;} Elseprintf ("the%d-pass output array is:", step0++);//    inti;  for(i=1; i<=n-1; i++) {printf ("%02d,", A[i]); } printf ("%02d\n", A[n]);}voidInsertsort (intA[],intN) {    inti,j,flag,step=0;//flag indicates Sentinelprint_arr1 (N,a,step);  for(i=2; i<=n;i++)    {        if(a[i]<a[i-1])//position less than when considering placing in a sub-sequence{flag=a[0]=a[i];//set up a lookouta[i]=a[i-1];  for(j=i-2; flag<a[j];--j) {A[j+1]=A[j]; } a[j+1]=Flag; } print_arr1 (N,a,++step); }}voidBubblesort (intB[],intN) {    inti,j,k=1, step=0;    PRINT_ARR2 (N,B,STEP,K);  for(i=n;i>=2; i--) {k=0;  for(j=n-1; j>=1; j--)        {            if(b[j]>b[j+1]) {k=1; Swap (b[j],b[j+1]); }} print_arr2 (N,b,++step,k); if(k==0)             Break; }}voidMerge (intC[],intD[],intSintMintt) {    inti=s,j,k=s;  for(i=s,j=m+1; i<=m&&j<=t;++k) {if(c[i]<C[j]) d[k]=c[i++]; ElseD[k]=c[j++]; }     while(i<=m) {d[k++]=c[i++]; }     while(j<=t) d[k++]=c[j++];  for(i=s;i<=t;i++)//Save the result in array d to C, or the C array will remain unchanged the next time you sortc[i]=d[i];}voidMergingsort (intC[],intD[],intSintt) {    if(s==t) d[s]=C[s]; Else    {        intM= (s+t)/2;        Mergingsort (C,D,S,M); Mergingsort (C,d,m+1, T);  Merge (c,d,s,m,t); //The c array is sorted into array d, and then the C array copies the D arrayPRINT_ARR3 (7, d);//the weakened version of the output will not follow    }    return ;}intMain () {intI,j,n; printf ("1.insertsort:\n"); inta[]={0, -,6,3, to,9, -,5, One}; N=8; printf ("sequence (13,6,3,31,9,27,5,11) is inserted directly into the ordering process as follows: \ n");    Insertsort (A,n); printf ("\n2. Bubblesort: \ n"); intb[]={0, +, -, the, -, -,8}; printf ("the specific implementation of the sequence t= (21,25,49,25,16,08) bubble sort is as follows: \ n"); N=6;    Bubblesort (B,n); printf ("\n3. Arrangesort: \ n"); intc[]={0, the, -, $, the, the, -, -}; intd[]={0, the, -, $, the, the, -, -}; printf ("the specific implementation of sequence t= (49,38,65,97,76,13,27) 2-way merge sort is as follows: \ n"); N=8; Mergingsort (C,d,1,8); return 0;}
View Code

Experiment Report--Direct insert sort, improved bubble sort and merge sort implement sorting of integer array

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.