Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Sorting a stack
- stack S = [226, 280, 462, 233, 254, 294, 93, 256, 315, 396];/* S is a stack implemented using array with last element in array as top of the stack. In this case top of the stack S is S[9] = 396 */
- sortStack(){
- if stack is not empty{
- int temp = S.pop();
- sortStack();
- sortedInsert(temp);
- }
- }
- sortedInsert(element){
- if stack is empty OR element > top element{
- S.push(element)
- }
- else{
- int temp = S.pop()
- sortedInsert(element)
- S.push(temp)
- }
- }
- main(){
- sortStack();
- // S will be sorted when control comes over here..
- }
Add Comment
Please, Sign In to add comment