Practical 1 (210280116514)
Practical 1 (210280116514)
Practical 1 (210280116514)
INDEX
Sr Practical Start End Sign
No. date date
Practical- 1
Aim: Implementation and Time analysis of sorting algorithms. Bubble sort,
Selection sort, Insertion sort, Merge sort and Quick sort.
>>Bubble Sort
Explanation:
Algorithm:
7. Now we will again set i=0 and continue with the same steps that will
eventually place second largest at second last place in the array. Now the
last 2 indexes of the array are sorted.
Analysis:
Time Complexity
Each and every element is compared with the other elements for array
which takes n time
And the above steps continues for n iterations
Best Time Complexity: O(n^2)
Average Time Complexity: O(n^2)
Worst Time Complexity: O(n^2)
Space Complexity
>>Selection Sort
Explanation:
Algorithm:
Analysis:
Time Complexity
In the worst case, in every iteration, we have to traverse the entire array
for finding min elements and this will continue for all n elements. Hence
this will perform n^2 operations in total.
In the best case that is sorted array, we can do some modification by
using lag to check whether the lament is already sorted or not
Best Time Complexity: O(n)
Average Time Complexity: O(n^2)
Worst Time Complexity: O(n^2)
Space Complexity
>>Insertion sort
Explanation:
Algorithm:
Take the first element and consider it to be a sorted part(a single element
is always sorted)
Now pick arr[1] and store it is a temporary variable
Start comparing the values of tmp with elements of the sorted part from
the rear side
If tmp is less than the rear element, say arr[k], then shift arr[k] to k+1
index
This shifting will continue until the appropriate location is identified.
Then, we will put the temporary element at the identified location
210280116514 Chavda Urvashi Analysis & Design of Algorithm
This will continue for all the elements, and we will have our desired
sorted array in ascending order
Analysis:
Time Complexity
In the worst-case scenario, n will pick all elements and then n shifts to set
it to the right position
In the best-case scenario, that is a sorted array, we will just pick the
elements, but no shifting will take place leading it to n time complexity,
that is, every element is traversed at least once
Best Time Complexity: O(n)
Average Time Complexity: O(n^2)
Worst Time Complexity: O(n^2)
Space Complexity
>>Merge sort
Explanation:
Algorithm:
Declare left and right var which will mark the extreme indices of the array
Left will be assigned to 0 and right will be assigned to n-1
Find mid = (left+right)/2
Call mergeSort on (left,mid) and (mid+1,rear)
Above will continue till left<right
210280116514 Chavda Urvashi Analysis & Design of Algorithm
Analysis:
Time Complexity
In the worst case, in every iteration, we are dividing the problem into
further 2 subproblems. Hence this will perform log n operations and this
has to be done for n iteration resulting in n log n operations total.
In the best case that is sorted array, we can do some modification by
using a flag to check whether the lament is already sorted or not
Best Time Complexity: O(nlogn)
Average Time Complexity: O(nlogn)
Worst Time Complexity: O(nlogn)
Space Complexity
>>Quicksort
210280116514 Chavda Urvashi Analysis & Design of Algorithm
Explanation:
Algorithm:
Analysis:
Time Complexity
Space Complexity