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