Sorting Algorithm (Java implementation) and Sorting Algorithm java Implementation

Source: Internet
Author: User

Sorting Algorithm (Java implementation) and Sorting Algorithm java Implementation

Over the past few days, I have been reading the data structure book by teacher Yan Weimin. I learned this for the first time. When I read it again, I found that it was very detailed and very good.

Then we will record the relevant sorting algorithms in the Java language that we are familiar. I think the following sorting algorithms are important and easy to take into account during the interview.

  • Bubble Sorting
  • Quick sorting
  • Heap sorting
  • Merge Sorting
(1) Preface

Sorting is actually a very big concept, mainly divided into two categories: Internal sorting and external sorting. What we usually call various sorting algorithms actually refer to internal sorting algorithms. Internal sorting is based on memory, and the entire sorting process is completed in memory. External sorting means that the memory cannot be fully occupied due to the large amount of data, sorting can be completed only through external storage (it is often calculated that some of the data that has been computed is removed from the memory to bring another portion of the uncomputed data into the memory ). This article mainly introduces several common sorting algorithms in inner sorting:

 

(2) Bubble Sorting

None of the most classic sorting methods is the first algorithm that everyone learns to sort.

The code is relatively simple.

 1 public class Main { 2     public static void main(String[] args) { 3          4         int[] a = {7, 6, 9, 3, 2, 11, 9}; 5         Bubble b = new Bubble(); 6         b.bubbleSort(a); 7         for (int i = 0; i < a.length; i++) { 8             System.out.print(a[i] + "  "); 9         }10         System.out.println();11     }12 }13 14 class Bubble {15     16     int[] bubbleSort(int[] a) {17         for (int i = 0; i < a.length - 1; i++) {18             for (int j = i + 1; j < a.length; j++) {19                 if (a[i] > a[j]) {20                     swap(a, i, j);21                 }22             }23         }24         return a;25     }26     27     void swap(int[] a, int i, int j) {28         int t = 0;29         t = a[i];30         a[i] = a[j];31         a[j] = t;32     }33 }

 

(3) quick sorting

One of the fastest sorting algorithms in history is fast sorting. Even the bottom layer of Arrays. sort is fast sorting, but some improvements have been made (dualpolictquicksort three thousand has more than one line of code, so it's so powerful)

The basic idea of fast sorting is to select an element from the sequence, but usually the first element of the sequence is directly selected as a standard, all the elements smaller than the element value are moved to the left of the element, and all the elements larger than the value are moved to the right of the element. This is called a fast sorting. The sub-tables on both sides of the element continue to carry out the fast Sorting Algorithm until the entire sequence is ordered. This sorting algorithm is currently the most efficient sorting algorithm in internal sorting. How does it work?

First, he defined two head and tail pointers pointing to the head and tail of the sequence respectively. Scan the entire sequence from the high pointer. If the element value pointed to by the high pointer is greater than or equal to the critical value, the pointer moves forward. If the value of the element pointed to by the high pointer is less than the critical value:

Exchange the small value pointed by the high pointer to the element value pointed by the low pointer, and move the low Pointer Forward.

Repeat the above process until x = 48 is less than him on the left and greater than him on the right. View code

 1 class Quick { 2      3     int[] quickSort(int[] a, int left, int right) { 4  5         if (left < right) { 6             int pos = postion(a, left, right); 7             quickSort(a, left, pos - 1); 8             quickSort(a, pos + 1, right); 9         }10         return a;11     }12     13     int postion(int[] a, int left, int right) {14         15         int t = a[left];16         while (left < right) {17             while (left < right && t <= a[right]) --right;18             a[left] = a[right];19             while (left < right && t >= a[left]) ++left;20             a[right] = a[left];21         }22         a[left] = t;23         return left;24     }25     26 }

 

(4) Heap sorting

First, let's look at what is heap.

A heap is a Complete Binary Tree of the following nature: the value of each node is greater than or equal to the value of its left and right child nodes; or the value of each node is smaller than or equal to the value of its left and right child nodes.

Big root heap: arr [I]> = arr [2i + 1] & arr [I]> = arr [2i + 2]

Small root heap: arr [I] <= arr [2i + 1] & arr [I] <= arr [2i + 2]

 

At the same time, we number the nodes in the heap by layer, and map this logical structure to the array as follows:

I think I understand heap sorting, but it is a bit difficult to write a blog. If you want to know it, you can go here and check the heap sorting algorithm. I think it is quite good.

I will use PriorityQueue directly here

1 class Heap {2 3 int [] heapSort (int [] a) {4 5 int [] t = new int [. length]; 6 PriorityQueue <Integer> pq = new PriorityQueue <> (. length); 7 // heap creation 8 for (int I = 0; I <. length; I ++) {9 pq. add (a [I]); 10} 11 int cnt = 0; 12 // if the definition of the heap is destroyed after the heap is exited, adjust the heap 13 while (! Pq. isEmpty () {14 t [cnt ++] = pq. poll (); 15} 16 return t; 17} 18}

 

 

(5) Merge Sorting

Here the merge sort algorithm refers to the merge sort. The core idea of merging and sorting is to recursively sort an initial sequence until there are few elements in the subsequence. Then, recursive return continues to directly sort the two separately ordered sequences. At the end of the recursion, the entire sequence must be ordered.Is divide and conquer

 

1/* recursive call of Merge Sorting */2 public static void MergeSort (int [] array, int low, int high) {3 if (low = high) {4 // indicates that the length of the sub-array is 1. If no decomposition is required, return 5} else {6 int p = (low + high)/2. 7 MergeSort (array, low, p); 8 MergeSort (array, p + 1, high); 9 // merge the adjacent two child sets 10 MergeTwoData (array, low, high ); 11} 12} 13/* Merge Sorting Algorithm for sorting two subsequences */14 public static void MergeTwoData (int [] array, int low, int high) {15 int [] arrCopy = new int [high-low + 1]; 16 int I, j; 17 I = low; j = (low + high)/2 + 1; 18 for (int key = 0; key <= high-low; key ++) {19 // if the length of the left-side sub-array is smaller than that of the right-side array, 20 if (I = (low + high)/2 + 1) {21 arrCopy [key] = array [j]; 22 j ++; 23} 24 // if the length of the array on the right is less than the length of the array on the left, after all the arrays on the right are written into the database, the array on the Left does not need to be compared to 25 else if (j = high + 1) {26 arrCopy [key] = array [I]; 27 I ++; 28} else if (array [I] <array [j]) {29 arrCopy [key] = array [I]; 30 I ++; 31} else {32 arrCopy [key] = array [j]; 33 j ++; 34} 35} 36 j = 0; 37 // write back the original array in Order 38 for (int x = low; x <= high; x ++) {39 array [x] = arrCopy [j]; 40 j ++; 41} 42}

 

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.