0% found this document useful (0 votes)
39 views

ADSAD

The document provides an assignment response that: 1. Compares and contrasts quicksort and mergesort, noting their worst time complexities as O(n^2) and O(n log n) respectively, with mergesort generally performing better for larger data sets. 2. Discusses that binary search is possible on a linked list but not optimal due to inability to randomly access elements. An algorithm to traverse a linked list is provided. 3. Provides an algorithm to find the second largest number in an unsorted array in O(n) time and O(1) space. 4. Provides an algorithm to print prime numbers in an array and count them in O(n^2)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

ADSAD

The document provides an assignment response that: 1. Compares and contrasts quicksort and mergesort, noting their worst time complexities as O(n^2) and O(n log n) respectively, with mergesort generally performing better for larger data sets. 2. Discusses that binary search is possible on a linked list but not optimal due to inability to randomly access elements. An algorithm to traverse a linked list is provided. 3. Provides an algorithm to find the second largest number in an unsorted array in O(n) time and O(1) space. 4. Provides an algorithm to print prime numbers in an array and count them in O(n^2)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Advanced Data Structures and Algorithm Design

Assignment: August 6th, 2021


Name:Kritika Sharma

1. Compare and contrast Quick sort and merge sort. Mention their worst time complexities. And
comment on which one is better on the basis of time complexity. Also, write algorithm of quick sort.
Ans. A) Compare and contrast Quick sort and merge sort

Sno Merge Sort Quick Sort


1. Partition of elements in The array is parted into just 2 The array is parted into any
the array halves (i.e. n/2). ratio.
2. Worst case complexity O(n log n). O(n2)
3. Usage with datasets Merge sort can work well on The quick sort cannot work well
any type of data sets with large datasets.
irrespective of its size (either
large or small).
4. Additional storage Merge sort is not in place The quick sort is in place as it
space requirement because it requires additional doesn’t require any additional
memory space to store the storage.
auxiliary arrays.
5. Efficiency Merge sort is more efficient Quick sort is more efficient and
and works faster than quick sort works faster than merge sort in
in case of larger array size or case of smaller array size or
datasets. datasets
6. Sorting method The merge sort is external The quick sort is internal sorting
sorting method in which the method where the data is sorted
data that is to be sorted cannot in main memory.
be accommodated in the
memory and needed auxiliary
memory for sorting.
7. Stability Merge sort is stable as two Quick sort is unstable in this
elements with equal value scenario. But it can be made
appear in the same order in stable using some changes in
sorted output as they were in code.
the input unsorted array.
8. Preferred for Merge sort is preferred for Quick sort is preferred for
linked lists. arrays.
9. Locality of reference Poor Good
B) Mention their worst time complexities.

Complexity Best case (Merge Sort) Average Worst

Time Complexity O(n log n) O(n log n) O(n log n)

Space Complexity O(n)

Complexity Best Case (Quick Sort) Average Worst

Time Complexity O(n) for 3 way partition or O(n log n) simple partition O(n log n) O(n2)

Space Complexity O(log n)

C) And comment on which one is better on the basis of time complexity.

In quick sort, using 3 way partitioning, it is possible to bring best case time complexity to O(n), which
will be better than merge sort best case performance of O(n log n).
In worst case, merge sort still maintains performance of O(n log n), but quick sort falls at O(n 2).
D) Also, write algorithm of quick sort.
This algorithm follows divide and conquer approach.
Algorithm
PARTITION (ARR, BEG, END, LOC)

Step 1: [INITIALIZE] SET LEFT = BEG, RIGHT = END, LOC = BEG, FLAG =0
Step 2: Repeat Steps 3 to 6 while FLAG !=1
Step 3: Repeat while ARR[LOC] <=ARR[RIGHT]
AND LOC != RIGHT
SET RIGHT = RIGHT - 1
[END OF LOOP]
Step 4: IF LOC = RIGHT
SET FLAG = 1
ELSE IF ARR[LOC] > ARR[RIGHT]
SWAP ARR[LOC] with ARR[RIGHT]
SET LOC = RIGHT
[END OF IF]
Step 5: IF FLAG = 0
Repeat while ARR[LOC] >= ARR[LEFT] AND LOC != LEFT
SET LEFT = LEFT + 1
[END OF LOOP]
Step 6:IF LOC = LEFT
SET FLAG = 1
ELSE IF ARR[LOC] < ARR[LEFT]
SWAP ARR[LOC] with ARR[LEFT]
SET LOC = LEFT
[END OF IF]
[END OF IF]
Step 7: [END OF LOOP]
Step 8: END

QUICK_SORT (ARR, BEG, END)

Step 1: IF (BEG < END)


CALL PARTITION (ARR, BEG, END, LOC)
CALL QUICKSORT(ARR, BEG, LOC - 1)
CALL QUICKSORT(ARR, LOC + 1, END)
[END OF IF]
Step 2: END

2. Is it possible to perform binary search on linked list? Write algorithm to traverse a linked list.
Ans. Binary search is possible on linked list data structure but there is no point using it, because at the
end it would take same time as normal linear search.

Binary search reduces the number of elements to be searched by filtering out almost half the number of
elements at each iteration. For the best implementation of Binary search we need two prerequisites:

1. The list must be in sorted order.


2. Any random element can be accessed in constant time.
With linked list the second prerequisite is not satisfied as any random element in linked list cannot be
accessed in constant time, but it must be traversed completely because each element in linked list is not
stored contiguously but in random locations and it could be accessed only by traversing the previous
element. (The previous element stores the address of the next element)

In an array basically any random element is accessed as,

Address of any random element = Base address of the array + index of the element*size of the element

The above calculation makes it possible to access any element in an array in constant time. So binary
search is preferred in array list, but not in linked list.

Conclusion:

As far as possibility of binary search in linked list is concerned, yes it is possible. But there is no proper
significance of it. Basically, it is unnecessary.

Algorithm to traverse a linked list.


STEP 1: SET PTR = HEAD
STEP 2: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 7
END OF IF

STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR != NULL


STEP 5: PRINT PTR→ DATA
STEP 6: PTR = PTR → NEXT
[END OF LOOP]

STEP 7: EXIT

3. Given an unsorted array of numbers, you have to search the 2nd largest number. Try to keep
time and space complexity minimum.
Ans.
Program:
public class Main {
public static void main(String[] args) {
int[] myNum = {54, 32, 67, 45};
int max1=0, max2=0;
int size = myNum.length;
System.out.println("array is of length:"+size);
for(int i=0;i<size;i++) {
if(myNum[i]>max1){
max2=max1;
max1=myNum[i];
}
}
System.out.println("2nd largest number is:"+max2);
}
}
Code test:

Time complexity: O(n)


Space complexity: O(n)
4. Given an array of unique numbers, print the prime numbers and total number of prime numbers
present in the given array.
Ans.
Program:
public class Main {
public static void main(String[] args) {
int[] myNum = {54, 7, 32, 3, 67, 45, 17};
int size = myNum.length, count = 0;
System.out.println("Prime numbers in array are:");
for (int i=0; i<size; i++){
if(isPrime(myNum[i])){
System.out.println(myNum[i]);
count++;
}
}
System.out.println("total number of prime numbers:"+count);
}

public static boolean isPrime(int N){


for (int i=2; i*i<N; i++){
if(N%i==0){
return false;
}
}
return true;
}
}

Code Test:
5. Define space complexity and time complexity.
Ans. Both are parameters to judge which solution is more optimal.
 Independent of the machine and its configuration, on which the algorithm is running on.
 Shows a direct correlation with the number of inputs.
 Can distinguish two algorithms clearly without ambiguity.

Space Complexity: Space complexity of an algorithm represents the amount of memory space needed the
algorithm in its life cycle.
Space needed by an algorithm is equal to the sum of the following two components
A fixed part that is a space required to store certain data and variables (i.e. simple variables and constants,
program size etc.), that are not dependent of the size of the problem.
A variable part is a space required by variables, whose size is totally dependent on the size of the
problem. For example, recursion stack space, dynamic memory allocation etc.

Time Complexity
Time Complexity of an algorithm is the representation of the amount of time required by the algorithm to
execute to completion. Time requirements can be denoted or defined as a numerical function t(N), where
t(N) can be measured as the number of steps, provided each step takes constant time.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy