Fast sorting and Merge Sorting

Source: Internet
Author: User

1. Fast sorting

Fast sorting: splits value columns and recursively sorts two parts to sort value columns. Its basic idea is: Split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method.

First, select any data (usually the first data) as the key data, and put all the numbers smaller than it in front of it, and all the numbers larger than it in front of it, this process is called quick sorting.

How can this problem be achieved? ---

The key lies in the location of the reference element after a fast sorting. Each time, the first element of a split column is selected as the reference element, to find its own location after it is used to split the sorting column, write

Int findpartition (data, Min, max) method, used to use data [Min] as the reference element to split data [Min] to data [Max] into two parts, returns the index of the location where the base element is located after the split.

Call the following in the main function:

`public static void quickSort(Comparable[] data,int min,int max) {int mid;if(min < max)        {            mid = findPartition(data,min,max);            quickSort(data,min,mid-1);            quickSort(data,mid+1,max);        }    }`

To understand the recursion process: If (Min <max) is to divide the value column into a single element (the deepest layer of recursion). After obtaining the position of the reference element min, the position of the base element in the array is final determined, and only the left and right sides of the split column are left
Sort: quicksort (data, Min, mid-1 );
Quicksort (data, Mid + 1, max );

The following describes how to implement the findpartition function:

Use the first element as the reference element, and set two pointers left and right. Move left from left to right to find a number larger than the reference element, right moves from right to left to find a number smaller than the base element. when both of them find the corresponding left and right positions
These two elements. Repeat this process until right <left. In addition to the reference element, the elements from min + 1 to right are smaller than the reference element, and the elements from left to Max are larger than the reference element. A sequence is formed:

Reference element (data [Min])... smaller than the reference element ......
Data [right]|
Data [left]... larger than the base element

You can obtain the line based on the baseline element by exchanging the benchmark element and data [right ].
Left and Right Split columns.

Here, I separate the call of sorting recursion from the split value column and the search for the split position (core part) into two methods to make the logic clearer.

Complete code:

`// Fast sorting: Fast sorting is a recursive process !!!!Public static void quicksort (comparable [] data, int min, int max ){Int mid;If (Min <max){Mid = findpartition (data, Min, max );Quicksort (data, Min, mid-1 );Quicksort (data, Mid + 1, max );}}// Supported methods for fast sortingPublic static int findpartition (comparable [] data, int min, int max ){Int left, right;Comparable temp, partitionelement;Left = min; Right = max;Partitionelement = data [Min]; // partitionelement is a copy of data [Min]        While (left <right){While (data [left]. compareto (partitionelement) <= 0 & left <right)Left ++;While (data [right]. compareto (partitionelement)> 0)Right --;If (left <right){Temp = data [left];Data [left] = data [right];Data [right] = temp;}}Temp = data [Min];Data [Min] = data [right];Data [right] = temp;// Incorrect syntax. You need to exchange the first element with data [right] instead of its copy.// Temp = data [right];// Data [right] = partitionelement;// Partitionelement = temp;        Return right;}`

2. Merge and sort

Merge Sorting is also a recursive implementation. The key lies in merging two ordered sets:

Void Merge (comparable [] data, int min, int mid, int max)

Array data is ordered from min to mid, and from Mid + 1 to max. These two parts are merged to make the order from min to Max, the ordered sequence is still placed in the subscript from min to max of data.

Recursive call:

`// Merge Sorting: merging is a recursive process. The key part is to merge two ordered sets.Public static void mergsort (comparable [] data, int min, int max ){Int mid = (min + max)/2;// Recursive processIf (max> min ){Mergsort (data, Min, mid );Mergsort (data, Mid + 1, max );Merge (data, Min, mid, max );}}`

Understanding the recursive process: after each MERG operation, data is sorted in order at the original position of data without affecting other locations.

The key part is as follows: Merge min to mid, Mid + 1 to max in data, and put the merged position on min to Max, apply for a space equal to the data size !!!

This is not clear at the beginning: it should be comparable [] temp = new
Comparable [Max + 1];

Instead of comparable [] temp = new comparable [max-min + 1]; think about why?

Merge will be called every recursion. In fact, temp will re-allocate the data every time, and use temp to sort the data in the order from min to Max and put it in the position of data.

Complete code:

`// Merge Sorting: merging is a recursive process. The key part is to merge two ordered sets.Public static void mergsort (comparable [] data, int min, int max ){Int mid = (min + max)/2;// Recursive processIf (max> min ){Mergsort (data, Min, mid );Mergsort (data, Mid + 1, max );Merge (data, Min, mid, max );}}// Supported Method: merge two Ordered Sets and apply for a space equal to the sum of the two ordered sets.Public static void Merge (comparable [] data, int min, int mid, int max ){Comparable [] temp = new comparable [Max + 1];// Comparable [] temp = new comparable [max-min + 1]; Error !!!Int begin1 = min;Int begin2 = Mid + 1;Int Index = min; // note that the index starts from Min !!!            While (begin1 <= Mid & begin2 <= max ){If (data [begin1]. compareto (data [begin2]) <0)Temp [index ++] = data [begin1 ++];ElseTemp [index ++] = data [begin2 ++];}If (begin1> mid)For (INT I = begin2; I <= max; I ++)Temp [index ++] = data [I];If (begin2> MAX)For (INT I = begin1; I <= mid; I ++)Temp [index ++] = data [I];For (INT I = min; I <= max; I ++) // corresponding location !!!!Data [I] = temp [I];}}`

This program has been debugged for a long time, but it is not clear about the recursive process. data cannot be overwritten in each recursive process, and the sorting part cannot work !!!

In addition, the temp application space is Max + 1, not the size of max-min + 1. Otherwise, the array may be out of bounds.

The Code was messy and tragic. I copied the code again, which took a long time.

Okay. If you have time, write a performance comparison summary.

Related Keywords:

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.