. Quick Sort
The quick sort (Quicksort) is an improvement on bubble sorting. by C. A. R. Hoare was introduced in 1962. Its basic idea is to divide the data to be sorted into two separate parts by a sort of a part of all the data is smaller than the other part of all the data, and then the two parts of the data for the rapid sorting, the entire sequencing process can be recursive, so as to achieve the entire data into an ordered sequence.
The quick
Code 1:
Copy Code code as follows:
function Quicksort ($STR) {
if (count ($str) $key = $str [0];//takes a value and is later used for comparison;
$left _arr=array ();
$right _arr=array ();
For ($i =1 $i if ($str [$i]$left _arr[]= $str [$i];
Else
$right _arr[]= $str [$i];
}
$left _arr=quicksort ($left _arr);//recursive;
$right _arr=quick
Just learn ruby, it happens algorithm teacher encourage in unfamiliar language to write algorithm, I use Ruby Bar ~ ~In other words, Ruby is really awesome, and it's a lot of intuitive way to use ... In the infinite worship ....
During the period I encountered a invalid multibyte char (US-ASCII) error, the solution was to add a #encoding:utf-8 at the beginningThe mistake was asked by someone on the StackOverflow, and the answer wasWrite # Encoding:utf-8 on the top of that file. That is changes
unordered decimal group, followed by execution, our array is ordered. is actually a recursive process. The quickSort () below is the process. First, the partition () method is not required to be split, and then the quickSort () method is called again to execute the first half, and the same call to the QuickSort () method executes the second half. The code is sho
Take a value and compare it with other values. put a small value on the left of the value, a large value on the right of the value, and then follow this method to recursively code 1:
The code is as follows:
Function quicksort ($ str ){
If (count ($ str) $ Key = $ str [0]; // Obtain a value for comparison later;
$ Left_arr = array ();
$ Right_arr = array ();
For ($ I = 1; $ I
If ($ str [$ I] $ Left_arr [] = $ str [$ I];
Else
$ Right_arr [] = $
To imagine such a requirement, we need to provide a sort component for our framework. Currently, Bubble Sorting is required.AlgorithmAccording to the idea of "interface-oriented programming", we can provide a unified interface isort for these sorting algorithms, in which there is a method sort (), it can accept an object array parameter. Returns the array after sorting the array. The interface is defined as follows:
Public interface isort{Void sort (ref object [] besorted );}
The class diagr
Public class quicksort {
/**
*Quick sorting
* @ Param strdate
* @ Param left
* @ Param right
*/
Public void quicksort (string [] strdate, int left, int right ){
String middle, tempdate;
Int I, J;
I = left;
J = right;
Middle = strdate [(I + J)/2];
Do {
While (strdate [I]. compareto (middle)
I ++ ;//Find the number on the left that is greater than the median.
While (strdate [J
the divide-and-conquer method to divide a string (list) into two substrings (sub-lists). The specific algorithm is described as follows:
Select an element from the series, called the "Datum" (pivot);
Reorder the columns, where all elements are placed in front of the datum in a smaller position than the base value, and all elements are larger than the base value behind the datum (the same number can be on either side). After the partition exits, the datum is in the middle of the seq
sorting:
The code is as follows:
#encoding: Utf-8
#author: Xu Jin, 4100213
#date: Oct 20, 2012
#RandomizedQuickSort
#to sort an array by using QuickSort
#example:
#The original Array is:[10, 35, 25, 67, 69, 52, 24, 40, 69, 76, 6, 49]
#The sorted array is: [6, 10, 24, 25, 35, 40, 49, 52, 67, 69, 69, 76]
Arrayint = Array.new
index = 0
while (Index
Arrayint[index] = rand (m) #produce random number
Index + + 1
End
Puts "The original arra
Ruby implements three fast sorting algorithms:
I just learned Ruby. It happened that the algorithm teacher encouraged me to write algorithms in unfamiliar languages. I will use Ruby ~~Ruby is really amazing, and many intuitive methods can be used ..... Infinite worship ....
While I encountered an invalid multibyte char (US-ASCII) error, the solution is to add a # encoding: UTF-8 at the beginningThis error was asked by someone on stackoverflow. The answer Someone gave is:Write # encoding: UTF-8 o
:*/
2.1 core code
bool BubbleSort::sort(void){ int i,j; for( i=0; i
2.2 Time complexity and Space complexity
/*** For sort algorithm, it's basic operation is swap two values. So we can compute it's sentence frequency f (n ):* F (n) = n * n = n ^ 2* (At worst situation)* And it's time complexity is:* T (n) = O (f (n) = O (n ^ 2)*** Obviously, It's space complexity is:* S (n) = O (g (n) = O (C) = O (1)* Because it use only constant space whatever n change.*/
/** The totally example sourc
than s
If I >= J, then leave the loop
If I
Swap the left axis with J
Recursive to the left of the axis
Recursive to the right of the axis
By performing the algorithm, the value on the left side of the axis will be less than s, and the value on the right side of the axis will be greater than s, so that the left and right sides of the axis are handed back, so that the order can be completed. In the above example, the value on the left side of 41 is smaller than it, and the val
Code Listing 1:
Copy the code code as follows:
function Quicksort ($STR) {
if (count ($str) $key = $str [0];//takes a value, which is later used for comparison;
$left _arr=array ();
$right _arr=array ();
for ($i =1; $i if ($str [$i]$left _arr[]= $str [$i];
Else
$right _arr[]= $str [$i];
}
$left _arr=quicksort ($left _arr);//recursive;
$right _arr=quicksort ($righ
Take a value and compare it with other values. put a small value on the left of the value, a large value on the right of the value, and then follow this method to recursively code 1:
The code is as follows:
Function quicksort ($ str ){If (count ($ str) $ Key = $ str [0]; // Obtain a value for comparison later;$ Left_arr = array ();$ Right_arr = array ();For ($ I = 1; $ I
If ($ str [$ I] $ Left_arr [] = $ str [$ I];
Else
$ Right_arr [] =
Void QuickSort (SeqList R, int low, int high)
{
// Fast sorting of R [low... high]
Int pivotPos; // the location of the benchmark record after Division
If (low {
// Sorting is required only when the Interval Length is greater than 1
Extends TPOs = Partition (R, low, high); // divide R [low... high]
QuickSort (R, low, pivotPos-1); // recursively sorts the left Interval
Q
# Include # Include # Include # Include # Include # Include Using namespace STD;
Template Class CMP // Abstract Operator class{Public:Virtual bool operator () (const T one, const T Other) = 0; // pure virtual function, overload operator ()};
Template Class lessthan: Public CMP {Public:Bool operator () (const T one, const T other){Return one }};
Template Class greatthan: Public CMP {Public:Bool operator () (const T one, const T other){Return one> Other;}};
Template Class sort // abstract so
:
The Code is as follows:
# Encoding: UTF-8
# Author: xu jin 4100213
# Date: Oct 20,201 2
# RandomizedQuickSort
# To sort an array by using QuickSort
# Example:
# The original array is: [10, 35, 25, 67, 69, 52, 24, 40, 69, 76, 6, 49]
# The sorted array is: [6, 10, 24, 25, 35, 40, 49, 52, 67, 69, 69, 76]
ArrayInt = Array. new
Index = 0
While (index
ArrayInt [index] = rand (100) # produce 12 random number
Index + = 1
End
Puts "The original array is:" +
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.