Implementation of sorting algorithm--Realization of understanding method

Source: Internet
Author: User

I have seen many of the principles of sorting algorithms, every time you want to realize it, have been dragged, and now the Cattle Network learning algorithm courses, I hope I can persist in practice.

//for an int array, write a selection bubbling algorithm that sorts the elements of the array. //given an int array A and the size of the array n, return the sorted array. //Test Examples://[1, 2, 3, 5, 2, 3], 6//[1, 2, 2, 3, 3, 5]#include<iostream>using namespacestd; #include<string>voidPrintresult (stringStrintAintN) {cout<< Str <<"result of: \ n";  for(inti =0; I < n; i++) {cout<< A[i] <<" "; } cout<<Endl;}voidSwapintAintb) {    inttemp=A; A=b; b=A;}classBubblesort { Public:    int* Bubblesort (intAintN) {//Write code here         for(inti =0; i<n; i++)        {             for(intj =0; J<n-i-1; J + +)            {                if(A[j]>a[j +1])                {                    inttemp =A[j]; A[J]= A[j +1]; A[j+1] =temp; }            }        }        returnA; }};//please write a selection sorting algorithmclassSelectionsort { Public:    int* Selectionsort (intAintN) {//Write code here        intK =0;  for(inti =0; I < n1; i++) {k=i;  for(intj = i; J < N; J + +)            {                if(a[k]>A[j]) {k=J; }            }            if(k!=i) {inttemp =A[i]; A[i]=A[k]; A[K]=temp; }        }        returnA; }};//please write a selection insertion algorithmclassinsertionsort{ Public:    int* Insertionsort (intAintN) { for(inti =1; I < n; i++)        {            inttemp =A[i]; intj = i-1;  for(; J >=0; j--)//the front of J is already lined up, from behind to the next, when there is no greater than the current value of the time bereak;            {                if(a[j]>temp) {A[j+1] =A[j]; }                Else                {                     Break; }} a[j+1] =temp; }        returnA; }};intMain () {intN =0; cout<<"number of sorted data: \ n"; CIN>>N; int* A =New int[N]; cout<<"Please enter the data to be sorted: \ n";  for(inti =0; i < N; i++) {cin>>A[i];    } Bubblesort bubble;    Bubble.bubblesort (A,n); Printresult ("Bubblesort", A, N); SelectionsortSelect; Select. Selectionsort (A, N); Printresult ("Selectsort", A, N);    Insertionsort Insert;    Insert.insertionsort (A, N); Printresult ("Insetsort", A, N); return 0;}

Continuous update ...

Implementation of sorting algorithm--Realization of understanding method

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.