Insert Sort:
like a card, touch a plug in, find a sentinel, from the second start, and the previous comparison, small words move forward one.
#include <iostream> #include <stdlib.h>using namespace std, #define N 4//No Semicolon End class Sort{public:void ins Ertsort (int a[],int n); Sort () {cout<< "Start" <<endl; } sort (const sort&s);p rivate:};void sort::insertsort (int a[],int n) {int i=1,j=0,key;//set two variable values for internal and external changes, if only one internal and external change The effect of the i<n//outer loop {key=a[i];//to take out the first few, and the front of the contrast; j=i-1; The front value while (key<a[j]&&j>=0)//inner loop {a[j+1]=a[j]; Change the current value to the previous value j--; Position move forward one} A[j+1]=key; After a successful comparison, the Sentinel is inserted into the current position, with the attention of J i++; Go to the Next loop} cout<< "-----------\ n"; for (int i=0;i<n;i++) {cout<<a[i]; }}sort::sort (const sort&s) {cout<< "news\n";} int main () {sort S; Sort b=s; int a[n]; cout<< "Lease input arr."; for (int i=0;i<n;i++) {cin>>a[i]; } cout<< "Shuchu Wei"; for (int i=0;i<n;i++) { cout<<a[i]<<endl; } s.insertsort (A,n); return 0;};
Bubble Sort:
<span style= "FONT-SIZE:14PX;" > #include <iostream> #include <stdlib.h>using namespace std, #define N 4//No semicolon End Class Sort{public:void Insertsort (int a[],int n); Sort () {cout<< "Start" <<endl; } sort (const sort&s); void Bubblesort (int a[],int n);p rivate:int c;}; void Sort::insertsort (int a[],int n) {int i=1,j=0,key;//sets two variable values for the purpose of internal and external changes, if only one internal and external changes will have an effect while (i<n)//outer Loop { Key=a[i];//took the first few out, and the front of the contrast; j=i-1; The front value while (key<a[j]&&j>=0)//inner loop {a[j+1]=a[j]; Change the current value to the previous value j--; Position move forward one} A[j+1]=key; After a successful comparison, the Sentinel is inserted into the current position, with the attention of J i++; Go to the Next loop} cout<< "-----insertsort------\ n"; for (int i=0;i<n;i++) {cout<<a[i]; } Cout<<endl;} void Sort::bubblesort (int a[],int n) {int i=0,j=n; for (; i<n-1;i++)//Outer loop {for (; j>i;j--)//inner loop, bubbles up from the last start, and the number of bubbles decreases by one { if (a[j]<a[j-1])//Countdown start, two numbers to compare, below is the exchange process {int temp=a[j-1]; A[J-1]=A[J]; A[j]=temp; }}} cout<< "-----bubblesort------\ n"; for (int i=0;i<n;i++) {cout<<a[i]; } Cout<<endl;} Sort::sort (const sort&s) {cout<< "news\n";} int main () {sort S; Sort b=s; int a[n]; cout<< "Lease input arr."; for (int i=0;i<n;i++) {cin>>a[i]; } cout<< "Shuchu Wei"; for (int i=0;i<n;i++) {cout<<a[i]; } cout<<endl; S.insertsort (A,n); S.bubblesort (A,n); return 0;}; </span><span style= "FONT-SIZE:24PX;" ></span>
Other algorithms to be continued ....
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Sorting algorithm C + + code implementation