Write an efficient non-recursive function to sort a stack of n elements

The elements must have a total order and the index of the array can be of any discrete type.

Write an efficient non-recursive function to sort a stack of n elements

History[ edit ] The quicksort algorithm was developed in by Tony Hoare while in the Soviet Unionas a visiting student at Moscow State University. At that time, Hoare worked in a project on machine translation for the National Physical Laboratory.

As a part of the translation process, he needed to sort the words of Russian sentences prior to looking them up in a Russian-English dictionary that was already sorted in alphabetic order on magnetic tape. He wrote a program in Mercury Autocode for the partition but could not write the program to account for the list of unsorted segments.

On return to England, he was asked to write code for Shellsort as part of his new job. Hoare mentioned to his boss that he knew of a faster algorithm and his boss bet sixpence that he did not.

His boss ultimately accepted that he had lost the bet. Later, Hoare learned about ALGOL and its ability to do recursion that enabled him to publish the code in Communications of the Association for Computing Machinerythe premier computer science journal of the time.

Hence, it lent its name to the C standard library subroutine qsort [7] and in the reference implementation of Java.

Robert Sedgewick 's Ph. Later Bentley wrote that he used Hoare's version for years but never really understood it but Lomuto's version was simple enough to prove correct.

Lomuto's partition scheme was also popularized by the textbook Introduction to Algorithms although it is inferior to Hoare's scheme because it does three times more swaps on average and degrades to O n2 runtime when all elements are equal. The shaded element is the pivot.

It is always chosen as the last element of the partition. Quicksort is a divide and conquer algorithm.

Mathematical induction.

Quicksort first divides a large array into two smaller sub-arrays: Quicksort can then recursively sort the sub-arrays. Pick an element, called a pivot, from the array. After this partitioning, the pivot is in its final position. This is called the partition operation.

Recursively apply the above steps to the sub-array of elements with smaller values and separately to the sub-array of elements with greater values.

The base case of the recursion is arrays of size zero or one, which are in order by definition, so they never need to be sorted. The pivot selection and partitioning steps can be done in several different ways; the choice of specific implementation schemes greatly affects the algorithm's performance.

Lomuto partition scheme[ edit ] This scheme is attributed to Nico Lomuto and popularized by Bentley in his book Programming Pearls [15] and Cormen et al. As this scheme is more compact and easy to understand, it is frequently used in introductory material, although it is less efficient than Hoare's original scheme.

In pseudocodea quicksort that sorts elements lo through hi inclusive of an array A can be expressed as: Hoare partition scheme[ edit ] The original partition scheme described by C. Hoare uses two indices that start at the ends of the array being partitioned, then move toward each other, until they detect an inversion: The inverted elements are then swapped.

There are many variants of this algorithm, for example, selecting pivot from A[hi] instead of A[lo]. Hoare's scheme is more efficient than Lomuto's partition scheme because it does three times fewer swaps on average, and it creates efficient partitions even when all values are equal.

Note that in this scheme, the pivot's final location is not necessarily at the index that was returned, and the next two segments that the main algorithm recurs on are lo. Implementation issues[ edit ] Choice of pivot[ edit ] In the very early versions of quicksort, the leftmost element of the partition would often be chosen as the pivot element.

Unfortunately, this causes worst-case behavior on already sorted arrays, which is a rather common use-case. The problem was easily solved by choosing either a random index for the pivot, choosing the middle index of the partition or especially for longer partitions choosing the median of the first, middle and last element of the partition for the pivot as recommended by Sedgewick.

Median-of-three code snippet for Lomuto partition: Similar issues arise in some other methods of selecting the pivot element. Repeated elements[ edit ] With a partitioning algorithm such as the ones described above even with one that chooses good pivot valuesquicksort exhibits poor performance for inputs that contain many repeated elements.

The problem is clearly apparent when all the input elements are equal: Consequently, the algorithm takes quadratic time to sort an array of equal values.

write an efficient non-recursive function to sort a stack of n elements

To solve this problem sometimes called the Dutch national flag problem [7]an alternative linear-time partition routine can be used that separates the values into three groups: Bentley and McIlroy call this a "fat partition" and note that it was already implemented in the qsort of Version 7 Unix.Objective: Given a binary tree, write a non recursive or iterative algorithm for Inorder traversal.

Example: Earlier we have seen ā€œ What is Inorder traversal and recursive algorithm for it ā€œ, In this article we will solve it with iterative/Non Recursive manner.

Given a list of ā€˜nā€™ elements the bubble sort requires up to n-1 passes to sort the data. Q&A for Previous Year Questions Subject: CPDS (barnweddingvt.com C language interview questions solution for freshers beginners placement tricky good pointers answers explanation operators data types arrays structures functions recursion preprocessors looping file handling strings switch case if else printf advance linux objective mcq faq online written test prime numbers Armstrong Fibonacci series factorial palindrome code programs examples on c++.

Given a stack, sort it using recursion. Use of any loop constructs like while, barnweddingvt.com is not allowed. We can only use the following ADT functions on Stack S: is_empty(S): Tests whether stack is empty or not. push(S): Adds new element to the stack.

pop(S): Removes top element from the stack. Recursion. The idea of calling one function from another immediately suggests the possibility of a function calling itself.

A permutation of n elements is one of the n! possible orderings of the elements. Checking if n is a Fibonacci number. Write a function to check if n is a Fibonacci number.

The merge sort is a recursive sort of order n*log(n). It is notable for having a worst case and average complexity of O(n*log(n)), and a best case complexity of O(n) (for pre-sorted input). The basic idea is to split the collection into smaller groups by halving it until the groups only have one element or no elements (which are both entirely sorted groups).

Quicksort - Wikipedia