You are on page 1of 9

AYUSH RAJ IIB2021012

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

A sorting algorithm is stable if it does not change the order of


elements with the same value.

EG. Bubble Sort, Selection Sort.

b) Just like the way bubbles rise from the bottom of a


glass, bubble sort is a simple algorithm that sorts a list,
allowing either lower or higher values to bubble up to the top.
The algorithm traverses a list and compares adjacent values,
swapping them if they are not in the correct order.

With a worst-case complexity of O(n^2), bubble sort is very slow


compared to other sorting algorithms like quicksort.

 Best case performance: O(n)


 Average case performance: O(n*n)
 Worst case performance: O(n*n)

PSEUDO CODE

procedure bubbleSort( list : array of items )

loop = list.count;

for i = 0 to loop-1 do:


swapped = false

for j = 0 to loop-1 do:

/* compare the adjacent elements */

if list[j] > list[j+1] then

/* swap them */

swap( list[j], list[j+1] )

swapped = true

end if

end for

/*if no number was swapped that means

array is sorted now, break the loop.*/

if(not swapped) then

break

end if

end for

end procedure return list


ITERATION

#include <stdio.h>

int BubbleSort(int array[], int n);

int main(void) {
int arr[] = {10, 2, 3, 1, 4, 5, 8, 9, 7, 6};
BubbleSort(arr, 10);

for (int i = 0; i < 10; i++) {


printf("%d", arr[i]);
}
return 0;
}
int BubbleSort(int array[], n)
{
for (int i = 0 ; i < n - 1; i++)
{
for (int j = 0 ; j < n - i - 1; j++) //n is length of array
{
if (array[j] > array[j+1]) // For decreasing order use
{
int swap = array[j];
array[j] = array[j+1];
array[j+1] = swap;
}
}
}
}

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.

/* low  –> Starting index,  high   –> Ending index */


quickSort(arr[], low, high) {
    if (low < high) {
        /* pi is partitioning index, arr[pi] is now at right place */
        pi = partition(arr, low, high);
        quickSort(arr, low, pi – 1);  // Before pi
        quickSort(arr, pi + 1, high); // After pi
  }
}

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);

for (int j = low; j <= high- 1; j++)


{
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

void printArray(int arr[], int size)


{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}

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]

Row- wise order,

AR [2] [3] = B + W(n(i-1) +(j-1)) 

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

therefore the matrix will look like

A[1][1]  A[1][2] ....................................  A[1][100]

                                                             A[2][99]

                                                       A[3][98]
                                             .

                                    .     

                              .

          A[98][3]

      A[99][2]

A[100][1]   A[100][2] ...............................A[100][100]

We want to go to element A[100][50]

means we need Row=100 and column=50

row1 has all 100 elements

row2 to 98 have 1 element only

row 100 has all 100 elements

therefore RMO = base + cross row1 + cross elements of row 2 to 98 +


cross 49 elements in row 100 to reach 50th element

                          =  1000 + 100 + 98 + 49

                         =  1247Bytes?

ANSWER6.

a) Partition the array on the basis of pivot: Call for partitioning


which rearranges the array in such a way that pivot (32) comes
to its actual position (of the sorted array). And to the left of the
pivot, the array has all the elements less than it, and to the right
greater than it.
o In the partition function, we start from the first element
and compare it with the pivot. Since 50 is greater than 32,
we don’t make any change and move on to the next
element 23.
o Compare again with the pivot. Since 23 is less than 32, we
swap 50 and 23. The array becomes 23, 50, 9, 18, 61, 32
o We move on to the next element 9 which is again less than
pivot (32) thus swapping it with 50 makes our array as 23,
9, 50, 18, 61, 32.
o Similarly, for next element 18 which is less than 32, the
array becomes 23, 9, 18, 50, 61, 32. Now 61 is greater than
pivot (32), hence no changes.
o Lastly, we swap our pivot with 50 so that it comes to the
correct position.
Thus the pivot (32) comes at its actual position and all elements to its
left are lesser, and all elements to the right are greater than itself.
Step 2: The main array after the first step becomes

23, 9, 18, 32, 61, 50

Step 3: Now the list is divided into two parts:


1. Sub list before pivot element
2. sub list after pivot element
Step 4: Repeat the steps for the left and right sub lists recursively.
The final array thus becomes

9, 18, 23, 32, 50, 61

b) PSEUDO CODE of Quick Sort

/**
* 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);

quickSort(arr, low, pivot_index - 1); // Before pivot_index


quickSort(arr, pivot_index + 1, high); // After pivot_index
}
}

c) The best-case complexity of the quick sort algorithm is


O(nlogn).

The worst-case time complexity of Quick Sort would be O(n2).

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.

You might also like