Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
quick_sort.cpp File Reference

Quick sort algorithm. More...

#include <cstdlib>
#include <iostream>
Include dependency graph for quick_sort.cpp:

Namespaces

namespace  sorting
 for working with vectors
 

Functions

int sorting::partition (int arr[], int low, int high)
 
void sorting::quickSort (int arr[], int low, int high)
 
void show (int arr[], int size)
 
int main ()
 

Detailed Description

Quick sort algorithm.

Implementation Details - Quick Sort is a divide and conquer algorithm. It picks and element as pivot and partition the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.

  1. Always pick the first element as pivot
  2. Always pick the last element as pivot (implemented below)
  3. Pick a random element as pivot
  4. Pick median as pivot

The key process in quickSort is partition(). Target of partition is, given an array and an element x(say) of array as pivot, put x at it's correct position in sorted array and put all smaller elements (samller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time

Function Documentation

◆ main()

int main ( void  )

Driver program to test above functions

82 {
83 int size;
84 std::cout << "\nEnter the number of elements : ";
85
86 std::cin >> size;
87
88 int *arr = new int[size];
89
90 std::cout << "\nEnter the unsorted elements : ";
91
92 for (int i = 0; i < size; ++i) {
93 std::cout << "\n";
94 std::cin >> arr[i];
95 }
96 quickSort(arr, 0, size);
97 std::cout << "Sorted array\n";
98 show(arr, size);
99 delete[] arr;
100 return 0;
101}
void quickSort(int arr[], int low, int high)
Definition: quick_sort.cpp:63

◆ show()

void show ( int  arr[],
int  size 
)
76 {
77 for (int i = 0; i < size; i++) std::cout << arr[i] << " ";
78 std::cout << "\n";
79}