Is this a variant of the bubble sort? Bubble sort and select sort

Source: Internet
Author: User

 

I learned the Sorting Algorithm and wrote one by myself. After writing the result, I found that it is different from the classic bubble algorithm.

The classic bubble algorithm first finds the largest one and puts it to the end.

I found the smallest one first and put it at the beginning. It should be a variant. Bubble

 

 
Void bubble1 (int * number, int count) // large down {int I, j, k = 0; // The number of cycles controls int m; // print the Array Control int temp; // Temporary Variable bool ischanged; for (I = count; I> 0; I --) {ischanged = false; printf ("bubble1% d results:", Count-I); For (j = 0; j <I-1; j ++) if (number [J]> Number [J + 1]) {temp = number [J]; number [J] = number [J + 1]; number [J + 1] = temp; ischanged = true;} If (! Ischanged) {break;} For (m = 0; m <count; m ++) printf ("% d,", number [m]); printf ("\ n");} printf ("bubble sorted"); printf ("["); For (INT I = 0; I <count; I ++) {printf ("% d,", number [I]);} printf ("]");}
 
 
Void bubble (int * number, int count) // small up {int I, j, k = 0; // The number of cycles controls int m; // print the Array Control int temp; // Temporary Variable bool ischanged; for (I = 0; I <count; I ++) {ischanged = false; printf ("bubble % d times result:", I); For (j = I + 1; j <count; j ++) if (number [I]> Number [J]) {temp = number [I]; number [I] = number [J]; number [J] = temp; ischanged = true;} If (! Ischanged) {break;} For (m = 0; m <count; m ++) printf ("% d,", number [m]); printf ("\ n");} printf ("bubble sorted"); printf ("["); For (INT I = 0; I <count; I ++) {printf ("% d,", number [I]);} printf ("]");}

Result

 

You can also choose sorting. This is classic.

The idea is to first find the smallest table and then exchange the table, which is more efficient.

Void select (int * number, int count) {int I, j, k = 0; // The number of cycles controls int m; // print the array to control int temp; // temporary variable for (I = 0; I <count; I ++) {k = I; printf ("select % d times result:", I ); for (j = I + 1; j <count; j ++) if (number [k]> Number [J]) k = J; If (K! = I) {temp = number [I]; number [I] = number [k]; number [k] = temp;} For (m = 0; m <count; M ++) printf ("% d,", number [m]); printf ("\ n");} printf ("sorted by select "); printf ("["); For (INT I = 0; I <count; I ++) {printf ("% d,", number [I]);} printf ("]");}

Result

 

Main Function

# Include <iostream> # include <string> # include <stdio. h> using namespace STD; Void Size (); void AVG (int * number, int count); void select (int * number, int count); void bubble (int * number, int count); void bubble1 (int * number, int count); void main () {int number [] = {11, 10, 9, 8, 7, 1, 5, 4, 3, 2, 6 }; // int number [] = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1}; int COUNT = sizeof (number)/sizeof (INT ); printf ("Count = % d \ n", count); // string B = NULL; AVG (number, count); // printf ("AVG = %. 3f \ n ", B); printf (" before Select sorting \ n "); printf (" ["); For (INT I = 0; I <count; I ++) {printf ("% d,", number [I]);} printf ("]"); printf ("\ n"); select (number, count); printf ("\ n"); // intnumber1 [] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; int number1 [] = {11, 2,, 1}; // int number1 [] = }; printf ("\ n" before Bubble Sorting); printf ("["); For (INT I = 0; I <count; I ++) {printf ("% d, ", number1 [I]);} printf ("] "); printf (" \ n "); bubble (number1, count); printf (" \ n "); // size (); int number2 [] = {,}; // int number1 [] =, 5, 4, 3, 2, 6}; printf ("\ n" before bubble1 sorting); printf ("["); For (INT I = 0; I <count; I ++) {printf ("% d,", number2 [I]);} printf ("]"); printf ("\ n"); bubble1 (number2, count ); printf ("\ n ");}

OK, the learning result of two days.

We also know that the number of bytes occupied by INT and char is sizeof (INT ).

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.