Eight sort algorithms that must be known [java Implementation] (1) bubble sort, fast sort, eight java

Source: Internet
Author: User

Eight sort algorithms that must be known [java Implementation] (1) bubble sort, fast sort, eight java

Bubble Sorting

Bubble Sorting is a simple sorting algorithm. It repeatedly visits the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted. The name of this algorithm comes from because the smaller elements will slowly "float" to the top of the series through the exchange.

Bubble SortingExample:

 

Bubble SortingAlgorithm ImplementationAs follows: [after sorting, arrays are arranged from small to large]

/* Bubble sort * compares adjacent elements. If the first is bigger than the second, exchange the two of them. * Perform the same operation on each adjacent element, starting from the first pair to the last one. At this point, the final element should be the largest number. * Repeat the preceding steps for all elements except the last one. * Repeat the above steps for fewer and fewer elements each time until there is no one to be compared. * @ Param numbers: the integer array to be sorted */public static void bubbleSort (int [] numbers) {int temp = 0; int size = numbers. length; for (int I = 0; I <size-1; I ++) {for (int j = 0; j <size-1-i; j ++) {if (numbers [j]> numbers [j + 1]) // exchanges two data locations {temp = numbers [j]; numbers [j] = numbers [j + 1]; numbers [j + 1] = temp ;}}}}

 

 

Quick sorting

Basic Idea of quick sorting:

Sort the records to be sorted into two separate parts. If the keywords of one part of the records are smaller than those of the other, sort the two parts separately, until the entire sequence is ordered.

Fast sortingExample:

(A) sorting process:

(B) the whole process of sorting

The whole sequence is regarded as an array, and the zero position is regarded as the central axis. If it is compared with the last one, it will not be processed if it is larger than it; after the exchange, we can compare it with a small client, which is smaller than it, but not more than it. In this way, after a round-robin is completed, the left side is smaller than the central axis, and the right side is bigger than the central axis. Then, the two independent arrays are sorted by Division and control.

 

The code is implemented as follows:

1. Find the position of the central axis (the vertical position is used as the central axis)

/*** Find the central axis (the default value is low) the position after sorting the numbers array ** @ param numbers to find the array * @ param low start position * @ param high end position * @ return axis position */public static int getMiddle (int [] numbers, int low, int high) {int temp = numbers [low]; // the first of the array is used as the central axis while (low 

 

2. Recursive grouping and sorting algorithms:

/***** @ Param numbers: Sorting array * @ param low start position * @ param high end position */public static void quickSort (int [] numbers, int low, int high) {if (low 

 

3. Quick sorting provides method calls

/*** Quick sorting ** @ param numbers comes with an array of sorting */public static void quick (int [] numbers) {if (numbers. length> 0) // check whether the array is null {quickSort (numbers, 0, numbers. length-1 );}}

 

Analysis:

Quick sorting is generally considered inSame order of magnitude (O (nlog2n ))The average performance of the sorting method is the best. However, if the initial sequence is ordered by the key code or is basically ordered, the quick sequence is sorted by bubble instead. For improvement, the benchmark record is usually selected based on "Three take China and France", that is, the center of the key codes of the two endpoints and the midpoint of the sorting interval is adjusted to the pivot record. Quick sorting is an unstable sorting method.

 

 Method Test

Print function:

  public static void printArr(int[] numbers)    {        for(int i = 0 ; i < numbers.length ; i ++ )        {        System.out.print(numbers[i] + ",");        }        System.out.println("");    }

 

Test:

Public static void main (String [] args) {int [] numbers = {10, 20, 15, 0, 6, 7, 2, 1,-5, 55}; System. out. print ("Before sorting:"); printArr (numbers); bubbleSort (numbers); System. out. print ("after Bubble Sorting:"); printArr (numbers); quick (numbers); System. out. print ("after fast sorting:"); printArr (numbers );}

 

Result:

Before sorting: 10, 20, 15, 0, 6, 7,-5, 55, after Bubble Sorting:-5, 0, 6, 7, 20, 55, after fast sorting:-5, 0, 6, 7, 10, 15, 20, 55,

 

Select sort, insert sort, Hill sort to view: http://www.cnblogs.com/0201zcr/p/4764427.html

Merge Sorting, heap sorting can be viewed: http://www.cnblogs.com/0201zcr/p/4764705.html

Thank you! Thank you for your patience!

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.