0% found this document useful (0 votes)
8 views11 pages

Vinay C Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views11 pages

Vinay C Lab

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CVR College of Engineering

Name:P.Vinay Kumar Goud


Roll no: 24B81A0328
Branch: mechanical
Subject: Problem solving through C(lab)
Topic: Sorting-Quick sort and merge sort
Sorting-Quick
sort and Merge
sort
What is sorting?
Sorting is the process of arranging elements in a specific
order.

It can be performed on various data types, including


numbers, strings, and objects.

It provides a structured way to organize data, making it


easier to analyze and interpret.

Many applications, such as databases and data processing


systems, rely on efficient sorting.
What is sorting?
Sorting is the process of arranging elements in a specific
order.

It can be performed on various data types, including


numbers, strings, and objects.

It provides a structured way to organize data, making it


easier to analyze and interpret.

Many applications, such as databases and data processing


systems, rely on efficient sorting.
Quick sort and its working
Quick sort is a divide-and-conquer
algorithm that sorts by partitioning an
array.

It selects a 'pivot' element and rearranges


the elements into two sub-arrays-
elements less than and greater than the
pivot element.

The process is recursively applied to the


sub-arrays until the entire array is sorted.
complexity
• Time Complexity: Space Complexity:
• Best Case: O(nlogn) Best case: O(log n)
Occurs when the pivot divides the array into Worst Case: O(n) due to n
two approximately equal halves. recursive calls if the recursion is
Average Case: O(nlogn) highly unbalanced.
Expected when the pivot selection is
balanced over all possible inputs.
Worst Case: O(n^2) Happens when the pivot
is always the smallest or largest
element
Example
For the array[10,7,8,9,1,5]

Step 1 When pivot element= 5


Partitioned
array=[1,5,8,9,7,10]
Step 2 When pivot element= 10
Partitioned
array=[1,5,8,9,7,10]

3
When pivot element= 7

4
When pivot element= 8
Partitioned Partitioned
Step array=[1,5,7,9,8,10] Step array=[1,5,7,8,9,10]
Merge sort
• Merge sort is another divide-and-
conquer algorithm that divides the
array into smaller segments.

• It recursively splits the array in half


until it reaches arrays of size one.

• These smaller arrays are then merged


back together in sorted order.

• The merging process compares


elements from each half and arranges
them in order.
complexity
• Time Complexity:
• Best/Average Case: O(n logn)
• Worst case: O(nlogn)

Space Complexity:
Auxiliary Space: O(n)
Temporary arrays are used for merging
subarrays.
Recursive Stack Space: O(logn)
Due to recursive function call
Example
For the array[12,11,13,5,6,7]

For left subarray [12,11,13]

Step 1 When mid index= 2


Left subarray=[12,11,13]
Right subarray=[5,6,7]
Step 2 When mid index= 1
Left subarray=[12,13]
Right subarray=[13]

For right subarray [5,6,7] Final merge;

Step 3 When mid index= 1


Left subarray=[5]
Right subarray=[6,7]
Step 4 Merged array:[11,12,13]
And [5,6,7]
Final array;[5,6,7,11,12,13]
THANK
YOU

You might also like