Data Structure essentials ------ bubble and direct sorting algorithms

Source: Internet
Author: User

As a programmer, we often use some simple sorting algorithms during program development, such as fast sorting of arrays. To achieve this, we need to apply the knowledge of Data Structure sorting algorithms, so proficient in using and mastering sorting algorithms is beneficial to developers. At the same time, record the knowledge points and further consolidate and summarize them:

So what is a sort algorithm?

Sorting is a kind of operation that is often performed in a computer. Its goal is to adjust a set of "unordered" record sequences to "ordered" Recording sequences. Sort by internal and external sorting.

So what is internal and external?

In-memory sorting: All data objects are stored in the memory for sorting.

Out-of-class sorting: this refers to the sorting that contains too many objects and cannot store memory at the same time. The Sorting must be continuously moved between internal and external storage according to the requirements of the dispatching process.

There are many inner sorting methods. Different policies can be classified into five types: insertion sorting, selection sorting, exchange sorting, Merge Sorting, and allocation sorting.

-- Swap sorting mainly includes Bubble sorting and quick sorting;

-- Insert sorting mainly includes direct insert sorting and Hill sorting;

-- Sorting mainly includes direct sorting and heap sorting;

-- Merge Sorting mainly includes two merge (Common Merge Sorting) and natural Merge Sorting.

-- Sort by allocation mainly includes box sorting and base sorting.

Stable sorting: if two or more records have the same keywords in the files to be sorted, if the relative order of these elements with the same keywords remains unchanged, This sorting method is stable. Among them, bubble, insert, base, and merge are stable sorting. Selection, fast, Hill, and heap are unstable sorting.

Time complexity is the most important indicator of the algorithm quality. The time complexity of sorting is closely related to the number of data comparisons in Algorithm Execution and the number of data moves.


Next, we will implement it step by step using code: first, we will practice two types of exchange sorting.

----- Bubble Sorting

Package COM. sort;/***** Bubble Sorting * @ author weixing-yang ** implementation idea: * first, compare the values of a [1] and a [2, if a [1] is greater than a [2], the values of the two are exchanged; otherwise, the values remain unchanged. * Compare the values of a [2] and a [3]. If a [2] is greater than a [3], the values of the two are exchanged. Otherwise, the values remain unchanged. * Compare the values of a [3] and a [4], and so on. Finally, compare the values of a [n-1] And a [n. * In this round, a [n] must be the largest in this group of data. * In a [1] ~ A [n-1] processes a round in the same way. * After a total of N-1 rounds are processed, a [1], a [2],..., and a [n] are arranged in ascending order. **/Public class bubblesort {int temp = 0; int I, j, k; Public void bubblesort (INT [] arr, int N) {// set I to the subscript for (I = n-1; I> 0; I = k) {for (k = J = 0; j <I; j ++) {If (ARR [J]> arr [J + 1]) {temp = arr [J]; arr [J] = arr [J + 1]; arr [J + 1] = temp; // If an exchange occurs, small subscript K = J ;}}}}}

----- Quick sorting

Package com. Sort;/***** @ author weixing-yang ** implementation idea: * separate the objects to be sorted into two separate parts by one sort. * The key codes of some objects are smaller than those of other objects. * Then, the two molecular sequence objects are sorted separately to achieve the whole sequence order. */Public class quicksort {public void quicksort (INT [] arr, int L, int R) {If (L> = r) return; int I = l, // from left to right cursor J = R + 1; // from right to left cursor int cursor = arr [l]; // use a [l] as the pivot int temp = 0; while (true) {While (ARR [++ I] <strong & I <R ); // find the & gt; = trim element on the left while (ARR [-- J] & gt; trim & amp; J & gt; L ); // find the <= signature element if (I> = J) break from the right; temp = arr [I]; arr [I] = arr [J]; arr [J] = temp;} // switch pivot a [l] to a [J]. Arr [l] = arr [J]; arr [J] = Beijing; // recursion if (L <r) quicksort (ARR, L, J-1); // quick sorting of the Left segment if (r> L) quicksort (ARR, J + 1, r); // fast sorting of the right segment }}


----- Test function

Package COM. sort; public class main {public static void main (string [] ARGs) {int [] array = {25, 36, 21, 45, 9, 40, 12, 34, 5, 55}; int n = array. length; bubblesort bubble = new bubblesort (); quicksort quick = new quicksort (); long start = system. currenttimemillis (); // bubble. bubblesort (array, n); // bubble sort quick. quicksort (array, 0, n-1); // fast sorting long end = system. currenttimemillis (); long sum = end-start; system. out. println ("Total number of milliseconds spent in sorting:" + sum); For (INT I = 0; I <array. length; I ++) {system. out. print (array [I] + "");}}}

----- Running result

Total number of milliseconds spent in sorting: 05 9 12 21 25 34 36 40 45 55

The time complexity of Bubble Sorting is N2, and the time complexity of fast sorting is nlogn.

Quick sorting is considered as the fastest inner sorting algorithm. When the data volume is small, fast sorting is even less than a simple sorting algorithm. In addition, when the data itself is ordered, the efficiency of fast sorting is extremely low.


@ -------> In the next article, we will continue to use the two sort algorithms for inserting sorting.


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.