Professional Documents
Culture Documents
ANSWER 2.
a) A sorting algorithm is In-place if the algorithm does not use extra
space for manipulating the input but may require a small though
nonconstant extra space for its operation.
EG. Selection Sort, Quick Sort
PSEUDO CODE
loop = list.count;
/* swap them */
swapped = true
end if
end for
break
end if
end for
#include <stdio.h>
int main(void) {
int arr[] = {10, 2, 3, 1, 4, 5, 8, 9, 7, 6};
BubbleSort(arr, 10);
QUICK SORT
Quick sort is an efficient divide and conquer sorting algorithm.
Average case time complexity of Quick Sort is O(nlog(n)) with worst
case time complexity being O(n^2) depending on the selection of the
pivot element.
ITERATION
#include<stdio.h>
void swap(int* a, int* b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
printf("Sorted array: n");
printArray(arr, n);
return 0;
}
ANSWER3.
a) 1 0 FALSE
20
20
b) 1 0 TRUE
00
00
ANSWER4.
AR [-4,…. 6, -2 … 12]
Given i = 2, j = 3, W = 2 bytes
B =?
n = Uc – Lc + 1
= 12 – (-2) + 1
= 12 + 2 + 1
= 15 Now, 414
= B + 2 [15 (2 -(-2)) + (3-(-4))]
B + 2[60 + 7]
= 4142 B + 134
= 4142 or B = 4008
ANSWER5.
RMO is used
A[2][99]
A[3][98]
.
.
.
A[98][3]
A[99][2]
= 1247Bytes?
ANSWER6.
/**
* The main function that implements quick sort.
* @Parameters: array, starting index and ending index
*/
quickSort(arr[], low, high)
{
if (low < high)
{
// pivot_index is partitioning index, arr[pivot_index] is now
at correct place in sorted array
pivot_index = partition(arr, low, high);
d) The average case run time of quick sort is O (n logn). This case
happens when we don’t exactly get evenly balanced partitions.
e) Quick sort is basically used to sort any list in fast and efficient
manner. Since the algorithm is in - place, quick sort is used
when we have restrictions in space availability too.