Sort algorithm from bubbling sort of thought of

Source: Internet
Author: User


1, the algorithm thought description narration:

1) Compare the adjacent two numbers, assuming that the previous one is larger than the one in the back, then they will be exchanged. Each cycle enables a number to reach an orderly state.


2. Complexity of Time:

Average O (n^2). Best: O (N), at the beginning of the sequence is a positive sequence of the time to obtain


3, implementation and optimization.

Here are three ways to implement

/* * bubblesort.cpp * * Created on:2014 May 17 * author:pc * * #include <iostream> #include <cstdio> #inclu De <ctime>using namespace Std;const int maxn = 10;int arr[maxn];/** * First switching algorithm: * With the help of intermediate variables */void swap1 (int arr[],int i , int j) {int temp = Arr[i];arr[i] = arr[j];arr[j] = temp;} /** * Another switching algorithm: * Without intermediate variables, using arithmetic operations */void swap2 (int arr[],int i, int j) {Arr[i] = Arr[i] + arr[j];arr[j] = arr[i]-Arr[j];ar R[i] = Arr[i]-arr[j];} /** * Third Exchange algorithm: * Use bitwise operation */void SWAP3 (int arr[], int i, int j) {Arr[i] = arr[i]^arr[j];arr[j] = Arr[i]^arr[j];arr[i] = Arr[i] ^ARR[J];} /** * The first way to bubble sort: * Standard way */void bubblesort1 (int arr[],int n) {int I;int j;for (i = 0; i < n; ++i) {//Loop n-1 times, each can be a number to an orderly state for (j = 0; j < n-i-1; ++j) {//each time the number before the first number to the ordered state is compared (the number after the ordered state is no longer required) if (Arr[j] > Arr[j+1]) {//assuming that the preceding number is greater than the subsequent number swap3 ( ARR,J,J+1);//The other way of exchanging}}}}/** * bubble sort: To optimize */void bubblesort2 (int arr[],int n) {int] by "Scanning again, if there is no exchange, that is to achieve an orderly State" feature K = N;bool flag = true;while (flag) {flag = false;for (int j = 0; j < k-1; ++J) {if (Arr[j] > Arr[j+1]) {swap3 (arr,j,j+1); flag = true;//is used to mark whether the interchange has occurred this time}}--k;}} /** * The third way to bubble sort: On another basis, for processing "If there are 100 data, if the last 90 are already ordered" */void bubblesort3 (int arr[],int n) {int K;int flag = N;while ( Flag > 0) {k = Flag;flag = 0;for (int j = 1; j < K; ++j) {if (arr[j-1] > Arr[j]) {swap3 (arr,j-1,j); flag = j;//change to mode 2 Into... Used to mark which position the interchange occurred}}}}void Createreversearr () {int i = 0;for (i = 0; i < MAXN; ++i) {arr[i] = maxn-i;}} void Printarr () {int i;for (i = 0; i < MAXN; ++i) {printf ("%d", Arr[i]);} printf ("\ n");} time_t printtime (String str) {time_t now = time (NULL), cout << str << ":" <<now <<endl;return now;} /** * Gets the system current time */time_t GetTime () {time_t now = times (NULL); int main () {Createreversearr ();p Rintarr (); time_t before = GetTime (); Bubblesort3 (ARR,MAXN); time_t after = GetTime (); Printarr ();cout<< "cost:" <<after-before << "s" &LT;&LT;ENDL;}



Sort algorithm from bubbling sort of thought of

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.