You are on page 1of 18

Pancake Sorting

/* A C++ program for Pancake Sorting */

#include <stdlib.h>

#include <stdio.h>

/* Reverses arr[0..i] */

void flip(int arr[], int i)

int temp, start = 0;

while (start < i)

temp = arr[start];

arr[start] = arr[i];

arr[i] = temp;

start++;

i--;

/* Returns index of the maximum element in arr[0..n-1] */

int findMax(int arr[], int n)

int mi, i;

for (mi = 0, i = 0; i < n; ++i)

if (arr[i] > arr[mi])

mi = i;

return mi;
}

// The main function that sorts given array using flip

// operations

int pancakeSort(int *arr, int n)

// Start from the complete array and one by one reduce

// current size by one

for (int curr_size = n; curr_size > 1; --curr_size)

// Find index of the maximum element in

// arr[0..curr_size-1]

int mi = findMax(arr, curr_size);

// Move the maximum element to end of current array

// if it's not already at the end

if (mi != curr_size-1)

// To move at the end, first move maximum number

// to beginning

flip(arr, mi);

// Now move the maximum number to end by reversing

// current array

flip(arr, curr_size-1);

}
}

/* A utility function to print an array of size n */

void printArray(int arr[], int n)

for (int i = 0; i < n; ++i)

printf("%d ", arr[i]);

// Driver program to test above function

int main()

int arr[] = {23, 10, 20, 11, 12, 6, 7};

int n = sizeof(arr)/sizeof(arr[0]);

pancakeSort(arr, n);

puts("Sorted Array ");

printArray(arr, n);

return 0;

}
Comb Sort

// C++ implementation of <a href="#">Comb Sort</a>

#include<bits/stdc++.h>

using namespace std;

// To find gap between elements

int getNextGap(int gap)

// Shrink gap by Shrink factor

gap = (gap*10)/13;

if (gap < 1)

return 1;

return gap;

// Function to sort a[0..n-1] using <a href="#">Comb Sort</a>

void combSort(int a[], int n)

// Initialize gap

int gap = n;

// Initialize swapped as true to make sure that

// loop runs

bool swapped = true;

// Keep running while gap is more than 1 and last


// iteration caused a swap

while (gap != 1 || swapped == true)

// Find next gap

gap = getNextGap(gap);

// Initialize swapped as false so that we can

// check if swap happened or not

swapped = false;

// Compare all elements with current gap

for (int i=0; i<n-gap; i++)

if (a[i] > a[i+gap])

swap(a[i], a[i+gap]);

swapped = true;

// Driver program

int main()

int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};

int n = sizeof(a)/sizeof(a[0]);
combSort(a, n);

printf("Sorted array: \n");

for (int i=0; i<n; i++)

printf("%d ", a[i]);

return 0;

}
Bubble sort

// C program for implementation of <a href="#">Bubble sort</a>

#include <stdio.h>

void swap(int *xp, int *yp)

int temp = *xp;

*xp = *yp;

*yp = temp;

// A function to implement <a href="#">bubble sort</a>

void bubbleSort(int arr[], int n)

int i, j;

for (i = 0; i < n-1; i++)

// Last i elements are already in place

for (j = 0; j < n-i-1; j++)

if (arr[j] > arr[j+1])

swap(&arr[j], &arr[j+1]);

/* Function to print an array */

void printArray(int arr[], int size)

int i;
for (i=0; i < size; i++)

printf("%d ", arr[i]);

printf("n");

// Driver program to test above functions

int main()

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr)/sizeof(arr[0]);

bubbleSort(arr, n);

printf("Sorted array: \n");

printArray(arr, n);

return 0;

}
TimSort

// C++ program to perform TimSort.

#include<bits/stdc++.h>

using namespace std;

const int RUN = 32;

// this function sorts array from left index to

// to right index which is of size atmost RUN

void insertionSort(int arr[], int left, int right)

for (int i = left + 1; i <= right; i++)

int temp = arr[i];

int j = i - 1;

while (arr[j] > temp && j >= left)

arr[j+1] = arr[j];

j--;

arr[j+1] = temp;

// merge function merges the sorted runs

void merge(int arr[], int l, int m, int r)

// original array is broken in two parts


// left and right array

int len1 = m - l + 1, len2 = r - m;

int left[len1], right[len2];

for (int i = 0; i < len1; i++)

left[i] = arr[l + i];

for (int i = 0; i < len2; i++)

right[i] = arr[m + 1 + i];

int i = 0;

int j = 0;

int k = l;

// after comparing, we merge those two array

// in larger sub array

while (i < len1 && j < len2)

if (left[i] <= right[j])

arr[k] = left[i];

i++;

else

arr[k] = right[j];

j++;

k++;
}

// copy remaining elements of left, if any

while (i < len1)

arr[k] = left[i];

k++;

i++;

// copy remaining element of right, if any

while (j < len2)

arr[k] = right[j];

k++;

j++;

// iterative Timsort function to sort the

// array[0...n-1] (similar to merge sort)

void timSort(int arr[], int n)

// Sort individual subarrays of size RUN

for (int i = 0; i < n; i+=RUN)

insertionSort(arr, i, min((i+31), (n-1)));


// start merging from size RUN (or 32). It will merge

// to form size 64, then 128, 256 and so on ....

for (int size = RUN; size < n; size = 2*size)

// pick starting point of left sub array. We

// are going to merge arr[left..left+size-1]

// and arr[left+size, left+2*size-1]

// After every merge, we increase left by 2*size

for (int left = 0; left < n; left += 2*size)

// find ending point of left sub array

// mid+1 is starting point of right sub array

int mid = left + size - 1;

int right = min((left + 2*size - 1), (n-1));

// merge sub array arr[left.....mid] &

// arr[mid+1....right]

merge(arr, left, mid, right);

// utility function to print the Array

void printArray(int arr[], int n)

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);


printf("\n");

// Driver program to test above function

int main()

int arr[] = {5, 21, 7, 23, 19};

int n = sizeof(arr)/sizeof(arr[0]);

printf("Given Array is\n");

printArray(arr, n);

timSort(arr, n);

printf("After Sorting Array is\n");

printArray(arr, n);

return 0;

}
Tree Sort

// C++ program to implement Tree Sort

#include<bits/stdc++.h>

using namespace std;

struct Node

int key;

struct Node *left, *right;

};

// A utility function to create a new BST Node

struct Node *newNode(int item)

struct Node *temp = new Node;

temp->key = item;

temp->left = temp->right = NULL;

return temp;

// Stores inoder traversal of the BST

// in arr[]

void storeSorted(Node *root, int arr[], int &i)

if (root != NULL)

storeSorted(root->left, arr, i);


arr[i++] = root->key;

storeSorted(root->right, arr, i);

/* A utility function to insert a new

Node with given key in BST */

Node* insert(Node* node, int key)

/* If the tree is empty, return a new Node */

if (node == NULL) return newNode(key);

/* Otherwise, recur down the tree */

if (key < node->key)

node->left = insert(node->left, key);

else if (key > node->key)

node->right = insert(node->right, key);

/* return the (unchanged) Node pointer */

return node;

// This function sorts arr[0..n-1] using Tree Sort

void treeSort(int arr[], int n)

struct Node *root = NULL;


// Construct the BST

root = insert(root, arr[0]);

for (int i=1; i<n; i++)

insert(root, arr[i]);

// Store inoder traversal of the BST

// in arr[]

int i = 0;

storeSorted(root, arr, i);

// Driver Program to test above functions

int main()

//create input array

int arr[] = {5, 4, 7, 2, 11};

int n = sizeof(arr)/sizeof(arr[0]);

treeSort(arr, n);

cout << "Sorted Array : ";

for (int i=0; i<n; i++)

cout << arr[i] << " ";

return 0;

}
counting sort

// C Program for counting sort


#include <stdio.h>
#include <string.h>
#define RANGE 255

// The main function that sort the given string arr[] in


// alphabatical order
void countSort(char arr[])
{
// The output character array that will have sorted arr
char output[strlen(arr)];

// Create a count array to store count of inidividul


// characters and initialize count array as 0
int count[RANGE + 1], i;
memset(count, 0, sizeof(count));

// Store count of each character


for(i = 0; arr[i]; ++i)
++count[arr[i]];

// Change count[i] so that count[i] now contains actual


// position of this character in output array
for (i = 1; i <= RANGE; ++i)
count[i] += count[i-1];

// Build the output character array


for (i = 0; arr[i]; ++i)
{
output[count[arr[i]]-1] = arr[i];
--count[arr[i]];
}

// Copy the output array to arr, so that arr now


// contains sorted characters
for (i = 0; arr[i]; ++i)
arr[i] = output[i];
}

// Driver program to test above function


int main()
{
char arr[] = "geeksforgeeks";//"applepp";

countSort(arr);

printf("Sorted character array is %sn", arr);


return 0;
}
bucket sort

// C++ program to sort an array using <a href="#">bucket sort</a>


#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

// Function to sort arr[] of size n using <a href="#">bucket sort</a>


void bucketSort(float arr[], int n)
{
// 1) Create n empty buckets
vector<float> b[n];

// 2) Put array elements in different buckets


for (int i=0; i<n; i++)
{
int bi = n*arr[i]; // Index in bucket
b[bi].push_back(arr[i]);
}

// 3) Sort individual buckets


for (int i=0; i<n; i++)
sort(b[i].begin(), b[i].end());

// 4) Concatenate all buckets into arr[]


int index = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < b[i].size(); j++)
arr[index++] = b[i][j];
}

/* Driver program to test above funtion */


int main()
{
float arr[] = {0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434};
int n = sizeof(arr)/sizeof(arr[0]);
bucketSort(arr, n);

cout << "Sorted array is \n";


for (int i=0; i<n; i++)
cout << arr[i] << " ";
return 0;
}

You might also like