C ++, C #, java Algorithm learning diary 01 --- BubbleSort)

Source: Internet
Author: User

C ++, C #, java Algorithm learning diary 01 --- BubbleSort)

Life is a process of continuous learning and perfection.C # Learning DiaryLet's get to an end (not to end ^_^). Now, let's start a new journey. Learn about algorithms!

When I learned C, I came into contact with the first algorithm in my life, BubbleSort ).

As the name suggests, the process of sorting array elements by bubble always places decimal places forward and big trees backward, similar to the action of bubbles rising in the water.

Basic Idea:

The basic idea of Bubble Sorting is to compare the values of adjacent elements. The values of the two elements are exchanged according to the rules of the smaller elements before the larger elements, finally, an incremental series is formed.

Let's take an example: we need to sort the series 63,4, 24,1, 3,15 in ascending order. We will use C ++, C #, and java respectively (this will happen in the future)

C ++ instance:

 

# Include
 
  
# Include
  
   
Using namespace std; // declare the output function void ShowArray (int * a, int L); // define the bubble function void BubbleSort (int * array, int Length) {for (int I = 1; I
   
    
Array [j + 1]) {// exchange int temp; temp = array [j]; array [j] = array [j + 1]; array [j + 1] = temp ;}}showarray (array, Length) ;}// output function void ShowArray (int * array, int Length) {for (int I = 0; I
    
     

 

 

C # example:
Using System; using System. collections. generic; using System. linq; using System. text; namespace BubbleSort {class Sort {// defines the Bubble Method public void BubbleSort (int [] array) {for (int I = 1; I <array. length; I ++) // control the number of loops {for (int j = 0; j <array. length-I; j ++) // if the two adjacent numbers meet the condition, they are exchanged {if (array [j]> array [j + 1]) {// int temp; temp = array [j]; array [j] = array [j + 1]; array [j + 1] = temp ;}} ShowArray (array );} // output method public void ShowArray (int [] array) {foreach (int I in array) {Console. write (I +);} Console. writeLine ();} static void Main (string [] args) {int [] array = new int [] {63,4, 24,1, 3,15}; Sort sorter = new Sort (); sorter. bubbleSort (array );}}}


The results of the above two instances:

Java instance:
package Sort;public class BubbleSort {public void sort(int[] array){for(int i=1;iarray[j+1]){int temp = array[j];array[j] = array[j+1];array[j+1]=temp;}}}showArray(array);}public void showArray(int[] array){for(int i:array){System.out.print(i+  );}System.out.println();}public static void main(String[] args) {int array[] = {63,4,24,1,3,15};BubbleSort sorter = new BubbleSort();sorter.sort(array);}}


Result:

 

 

This is the case for Bubble sorting. Several Common sorting methods will be introduced later. If there are any errors, please refer to ^_^.

 

Related Article

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.