CMPT 225 implementations

CMPT 225 implementations - = ipA[iUpper]; ipA[iUpper] =...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Quick sort implementation: #include <iostream> #include <ctime> void Partition(int* ipA, int iSize) { // Partitions of size 0 or 1 are already sorted if (iSize <= 1) { return; } // Select a pivot from the array randomly int iPivot = ipA[rand() % iSize]; // Indices of the entries to be swapped int iLower = 0; int iUpper = iSize - 1; // Partition array into sections above and below the pivot while (iLower < iUpper) { while (ipA[iLower] < iPivot) { ++iLower; } while (ipA[iUpper] > iPivot) { --iUpper; } // Swap the entries at the lower and upper indices int iTemp = ipA[iLower]; ipA[iLower]
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: = ipA[iUpper]; ipA[iUpper] = iTemp; } // Recursively call partition on each partititon. Partition(ipA, iLower); Partition(&amp;(ipA[iLower + 1]), iSize - iUpper - 1); } void Quicksort(int* ipA, int iSize) { // Seed the random number generator srand((unsigned int)time(0)); Partition(ipA, iSize); } int main() { using namespace std; int iaArray = {24, 5, 3, 35, 14, 23, 19, 43, 2}; int iSize = 9; Quicksort(iaArray, iSize); // Output sorted array for (int i = 0; i &lt; iSize; i++){ cout &lt;&lt; iaArray[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl; return 0; }...
View Full Document

Page1 / 2

CMPT 225 implementations - = ipA[iUpper]; ipA[iUpper] =...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online