Introduction to algorithms sorting algorithms

Source: Internet
Author: User

Read some of the code written in introduction to algorithms and make a record.


[Cpp]
# Include <iostream>
# Include <cmath>
# Include <vector>
# Define maxNumber 100000000;
/*------------------------------------------------------------------------
* Some algorithms involved in Chapter 2-7 of Introduction to algorithms:
* Insert sorting + Merge Sorting + heap sorting + quick sorting
* Version1.0: 2013/04/27
* Version2.0 goals: 1. Add other sorting algorithms: Bubble Sorting + Count sorting
* 2. Added support for more types, not limited to std: vector <int>
*------------------------------------------------------------------------*/
 
// ------------ Insert the Sorting Algorithm -----------------
Void InsertionSort (std: vector <int> & ){
Int len = A. size ();
Int I;
For (int j = 1; j <len; ++ j ){
Int key = A [j];
I = J-1;
While (I> = 0 & A [I]> key ){
A [I + 1] = A [I];
-- I;
}
A [I + 1] = key;
}
};
 
 
// ------------ Merge and sort ---------------------
Void Merge (std: vector <int> & A, int p, int q, int r ){
Int n1 = q-p + 1;
Int n2 = r-q;
Std: vector <int> L (n1 + 1 );
Std: vector <int> R (n2 + 1 );
Int I, j;
For (I = 0; I <n1; ++ I) L [I] = A [p + I];
For (j = 0; j <n2; ++ j) R [j] = A [q + j + 1];
L [n1] = maxNumber;
R [n2] = maxNumber;
I = 0;
J = 0;
For (int k = p; k <= r; ++ k ){
If (L [I] <= R [j]) {
A [k] = L [I];
++ I;
}
Else {
A [k] = R [j];
++ J;
}
}
};
 
Void MergeSort (std: vector <int> & A, int p, int r ){
Int q;
If (p <r ){
Q = (int) floor (double (p + r)/2 ));
MergeSort (A, p, q );
MergeSort (A, q + 1, r );
Merge (A, p, q, r );
}
};
 
 
// ------------ Heap sorting --------------------------
Int LEFT (int I ){
Return (2 * (I) + 1); // unlike in the book, C ++ starts from 0.
};
Int RIGHT (int I ){
Return (2 * (I) + 2 );
};
 
Void MaxHeapify (std: vector <int> & A, int I, int heap_size ){
Int l = LEFT (I );
Int r = RIGHT (I );
Int largest;
If (l Largest = l;
Else
Largest = I;
If (r Largest = r;
If (largest! = I ){
Std: swap (A [I], A [largest]);
MaxHeapify (A, largest, heap_size );
}
};
 
Void BuildMaxHeap (std: vector <int> & ){
Int heap_size = A. size ();
For (int I = (int) floor (double (A. size ()/2); I> = 0; -- I)
MaxHeapify (A, I, heap_size );
}
 
Void HeapSort (std: vector <int> & ){
BuildMaxHeap ();
Int heap_size = A. size ();//???
For (int I = A. size ()-1; I> = 1; -- I ){
Std: swap (A [0], A [I]);
Heap_size = heap_size-1;
MaxHeapify (A, 0, heap_size );
}
}
 
 
// ------------ Quick sorting -----------------------------
Int Partition (std: vector <int> & A, int p, int r ){
Int x = A [r];
Int I = p-1;
For (int j = p; j <r; ++ j ){
If (A [j] <= x ){
++ I;
Std: swap (A [I], A [j]);
}
}
Std: swap (A [I + 1], A [r]);
Return I + 1;
};
 
Void QuickSort (std: vector <int> & A, int p, int r ){
If (p <r ){
Int q = Partition (A, p, r );
QuickSort (A, p, q-1 );
QuickSort (A, q + 1, r );
}
};

# Include <iostream>
# Include <cmath>
# Include <vector>
# Define maxNumber 100000000;
/*------------------------------------------------------------------------
* Some algorithms involved in Chapter 2-7 of Introduction to algorithms:
* Insert sorting + Merge Sorting + heap sorting + quick sorting
* Version1.0: 2013/04/27
* Version2.0 goals: 1. Add other sorting algorithms: Bubble Sorting + Count sorting
* 2. Added support for more types, not limited to std: vector <int>
*------------------------------------------------------------------------*/

// ------------ Insert the Sorting Algorithm -----------------
Void InsertionSort (std: vector <int> & ){
Int len = A. size ();
Int I;
For (int j = 1; j <len; ++ j ){
Int key = A [j];
I = J-1;
While (I> = 0 & A [I]> key ){
A [I + 1] = A [I];
-- I;
}
A [I + 1] = key;
}
};


// ------------ Merge and sort ---------------------
Void Merge (std: vector <int> & A, int p, int q, int r ){
Int n1 = q-p + 1;
Int n2 = r-q;
Std: vector <int> L (n1 + 1 );
Std: vector <int> R (n2 + 1 );
Int I, j;
For (I = 0; I <n1; ++ I) L [I] = A [p + I];
For (j = 0; j <n2; ++ j) R [j] = A [q + j + 1];
L [n1] = maxNumber;
R [n2] = maxNumber;
I = 0;
J = 0;
For (int k = p; k <= r; ++ k ){
If (L [I] <= R [j]) {
A [k] = L [I];
++ I;
}
Else {
A [k] = R [j];
++ J;
}
}
};

Void MergeSort (std: vector <int> & A, int p, int r ){
Int q;
If (p <r ){
Q = (int) floor (double (p + r)/2 ));
MergeSort (A, p, q );
MergeSort (A, q + 1, r );
Merge (A, p, q, r );
}
};


// ------------ Heap sorting --------------------------
Int LEFT (int I ){
Return (2 * (I) + 1); // unlike in the book, C ++ starts from 0.
};
Int RIGHT (int I ){
Return (2 * (I) + 2 );
};

Void MaxHeapify (std: vector <int> & A, int I, int heap_size ){
Int l = LEFT (I );
Int r = RIGHT (I );
Int largest;
If (l Largest = l;
Else
Largest = I;
If (r Largest = r;
If (largest! = I ){
Std: swap (A [I], A [largest]);
MaxHeapify (A, largest, heap_size );
}
};

Void BuildMaxHeap (std: vector <int> & ){
Int heap_size = A. size ();
For (int I = (int) floor (double (A. size ()/2); I> = 0; -- I)
MaxHeapify (A, I, heap_size );
}

Void HeapSort (std: vector <int> & ){
BuildMaxHeap ();
Int heap_size = A. size ();//???
For (int I = A. size ()-1; I> = 1; -- I ){
Std: swap (A [0], A [I]);
Heap_size = heap_size-1;
MaxHeapify (A, 0, heap_size );
}
}


// ------------ Quick sorting -----------------------------
Int Partition (std: vector <int> & A, int p, int r ){
Int x = A [r];
Int I = p-1;
For (int j = p; j <r; ++ j ){
If (A [j] <= x ){
++ I;
Std: swap (A [I], A [j]);
}
}
Std: swap (A [I + 1], A [r]);
Return I + 1;
};

Void QuickSort (std: vector <int> & A, int p, int r ){
If (p <r ){
Int q = Partition (A, p, r );
QuickSort (A, p, q-1 );
QuickSort (A, q + 1, r );
}
};


 

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.