Introduction to common sorting algorithms

Insertion sort(Insertion sorting): Insert a piece of data into ordered data to form new ordered data with a length plus one. It is suitable for sorting a small amount of data. The time complexity is O(n^2), which is a stable sorting method.

Select sort(Selection sorting): Each time the smallest (or largest) element is selected from the data elements to be sorted, and stored at the beginning of the sequence, knowing that all the data elements to be sorted are exhausted, which is an unstable sorting method.

Heap sort(Heap Sort): Improved selection sorting. Construct the disordered number sequence into a pile, select the large top pile or the small top pile according to the requirements of ascending and descending order; swap the top element with the end element, and "sink" the largest element to the end of the array; readjust the structure; readjust the structure, Make it meet the heap definition, and then continue to exchange the top element and the current end element, and repeat the adjustment + exchange steps until the entire sequence is in order.

Bubble Sort(Bubble sorting): Compare adjacent elements in pairs, and exchange in the opposite order. Each trip will "float" the smallest or largest element to the top, and finally achieve complete order.

public static void bubbleSort(int[] a) {
for(int i=a.length-1; i>0; i--) { // n-1 scans
for(int j=0; j<i; j++) { 
if(a[j]>a[j+1]) {
swap(a, j, j+1);
}
}
}
}

private static void swap(int[] a, i, j) {
if(i = j) {
return;
}
int temp = a[i];
a[i] = a[j];
a[j] = a[i];
}

Quick sort(Quick sort): Improvements to bubble sorting. Split the data to be sorted into two parts that are smaller and larger than the selected data through one sorting, and then sort the two parts of data quickly according to this method.

Merge sort(Merge Sort): A divide and conquer strategy is adopted to merge existing ordered subsequences to obtain a completely ordered sequence. If two ordered lists are merged into one ordered list, it is called two-way merge.

Hill sort(Shell Sort): Also known as reduced incremental sort. The records are grouped by a certain increment of the target, and each group is sorted by the direct insertion sorting algorithm; as the increment decreases, each group contains more and more keywords. When the increment decreases to 1, the entire file is divided into one Group, the algorithm terminates.

Intelligent Recommendation

Introduction to Algorithms - Heap Sorting

First, let's talk about the characteristics of the heap data structure: the heap is an array, which can be regarded as an approximate complete binary tree. An index is an array of zeros. The parent no...

Introduction to the Eight Sorting Algorithms

Introduction to the Eight Sorting Algorithms Insert sort *Direct insertion sort *Hill sort 2. Select sort *Simple selection sort * heap sorting 3. Exchange sort *Bubble Sort *Quick sort 4. Sorting by ...

[Introduction to Algorithms] Merging and Sorting

1. Divide and conquer: The divide and conquer model has three steps in each layer of recursion:   a.break downThe original question is a number of sub-questions that are smaller examples of the origin...

Introduction to Algorithms - Quick Sorting

First, the algorithm description Quick sorting is a sorting algorithm based on divide and conquer. Three steps: Decomposition: The array A[p..r] is divided into two sub-arrays A[p..q-1] and A[q+1..r],...

[Introduction to Algorithms] Topological Sorting

Topological sorting First, the adjacency list (no predecessor implementation) Each step of the method always outputs a vertex that currently has no predecessor (ie, zero indegree) Its abstract algorit...

More Recommendation

Introduction to Algorithms Sorting

Counting Sort The small bucket sort used for data, the data amount of the case Linear Algorithm for selecting large numbers i...

Introduction to Algorithms - sorting

Sorting is considered the most basic problems in the study of algorithms. Often said that there is sort algorithm insertion sort, merge sort, heap sort, quick sort, counting sort, radix sort and bucke...

Introduction to Algorithms-Sorting

Looking at the introduction to algorithms for the second time, I feel that I have a better grasp of the essence of this book. Here, I will summarize the sorting algorithms that I think are more import...

Introduction to basic sorting algorithms

1. The definition of sorting The so-called sorting is the operation of arranging a string of data in increasing or decreasing order according to the size of one or some keywords. Sorting algorithm is ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top