Question

Given the array A = (5, 7, 14, 8, 11, 15, 9, 13, 12, 10), show how the Insertion sort and Quicksort algorithms work. Step thr

0 0
Add a comment Improve this question Transcribed image text
Answer #1

QuickSort algorithm is an inplace sorting algorithm it takes no aditional space to sort an array along with that it is also a divide and conquer technique

void QuickSort(int ar[],int l,int h)

{

if(l<h)

{

int pivot = parition(ar,l,h);

QuickSort(ar,l,p);

QuickSort(ar,p+1,h);

}

}

in each step of quick sort it finds a pivot element at places it at it's correct position in array and from that point it parititions the problem in two parts , all elements left to pivot and all element right to pivot element. The total time complexity of QuickSort is O(N2) in the worst case which is when data is already in descending order and best case time complexity is O(NlogN) which is when data is not in ascending or descending order.

A= {5,7,14,8,11,15,9,13,12,10}

steps of quicksort BOLD REPRESENTS THE SWAPPED ELEMENT

14 is selected as pivot than 10,11,12;

{5 7 14 8 11 15 9 13 12 10} {5 7 8 9 10 15 14 13 12 11} {5 7 8 9 10 11 14 13 12 15} {5 7 8 9 10 11 12 13 14 15}

INSERTION SORT

Insertion sort is also inplace sorting technique it does not follow divide and conquer Technique it compares the a new element in array to the already sorted array so far. The worst case time complexity of insertion sort is O(N2) and best case time complexity is O(N)

STEPS OF INSERTION SORT

bold signifies the sorted array at each step the insertion sort maintains a sorted array at each step , it picks element one by one from start and keep's on building a sorted array until the entire array becomes sorted

{5 7 14 8 11 15 9 13 12 10 } {5 7 14 8 11 15 9 13 12 10 } {5 7 8 14 11 15 9 13 12 10} {5 7 8 11 14 15 9 13 12 10 } {5 7 8 11 14 15 9 13 12 10} {5 7 8 9 11 14 15 13 12 10 } {5 7 8 9 11 13 14 15 12 10} {5 7 8 9 11 12 13 14 15 10} {5 7 8 9 10 11 12 13 14 15}
Add a comment
Know the answer?
Add Answer to:
Given the array A = (5, 7, 14, 8, 11, 15, 9, 13, 12, 10), show...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT