0% found this document useful (0 votes)
23 views8 pages

ADSA Exp2

Uploaded by

jam.explore.1
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)
23 views8 pages

ADSA Exp2

Uploaded by

jam.explore.1
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/ 8

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

WORKSHEET :2
Student Name: Piyush Patel UID: 23BCS12673
Branch: BE-CSE Section/Group: 717/B
Date of Performance: 27/08/2024 Semester: III
Subject Name: Advanced DSA Subject Code: 23CSH-204

1.Aim
1.1 In a sports tournament you need to rank competitors based on their scores the dataset is
large and you are required to determine most efficient based on time complexity for different
types of input data.
Note: You can take any two sorting techniques for comparison.

1.2 Hacker Rank Question: “Quicksort 1 – Partition”


Choose some pivot element, p, and partition your unsorted array, arr, into three smaller arrays:
left, right and equal where each element in left< p, each element in right> p, and each element
in equal = p.
Input Format:
The first line contains, the size of arr. The second line contains n space-separated
integers arr[i] (the unsorted array). The first integer, arr[0], is the pivot element.
Function Description:
Complete the quickSort function in the editor below. quickSort has the following parameter(s):
• int arr[n]: arr[0] is the pivot element
Returns:
• int[n]: an array of integers as describes above.
Constraints:
• 1<=n<=1000
• -1000<= arr[i] <=1000, where 0 <= i < n
• All elements are distinct.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

2.Source Code:
Solution 1.1
#include<iostream>
using namespace std;
void swap(int &a, int &b){
int temp=a;
a=b;
b=temp;
}
int partition(int *arr, int start, int end){
int pivot=arr[start];
int count=0;
for(int i=start;i<=end;i++){
if(arr[i]<pivot){
count++;
}
}
int pivotIndex=count+start;
swap(arr[start],arr[pivotIndex]);
int i=start, j=end;
while(i<pivotIndex && j>pivotIndex){
while(arr[i]<=pivot){
i++;
}
while(arr[j]>pivot){
j--;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

if(i<pivotIndex && j>pivotIndex)


swap(arr[i++],arr[j--]);
}
return pivotIndex;
}
void quickSort(int *arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex-1);
quickSort(arr, pivotIndex+1, high);
}
}
void merge(int *arr, int left, int mid, int right){
int size1 = mid - left + 1;
int size2 = right - mid;
int leftArray[size1], rightArray[size2];
for(int i = 0; i < size1; i++){
leftArray[i] = arr[left + i];
}
for(int j = 0; j < size2; j++){
rightArray[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = left;
while(i < size1 && j < size2){
if(leftArray[i] <= rightArray[j]){
arr[k] = leftArray[i];
i++;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

else{
arr[k] = rightArray[j];
j++;
}
k++;
}
while(i < size1){
arr[k] = leftArray[i];
i++;
k++;
}
while(j < size2){
arr[k] = rightArray[j];
j++;
k++;
}
delete []firstArray;
delete []secondArray;
}
void mergeSort(int *arr, int left, int right){
if(left < right){
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

int main() {
int arr[20]={11,24,45,23,1,4};
int n=6;
cout << "Original array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
quickSort(arr, 0,n-1 );
cout << "Sorted array by (quick Sort): ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
mergeSort(arr,0,n-1);
cout << "Sorted array by (MergeSort): ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

Solution 1.2
#include<iostream>
#include<vector>
Using namespace std;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

vector<int> quickSort(vector<int> arr) {


if(arr.size() <=1){
return arr;
}
int pivot = arr[0];
vector<int> left, equal, right;
for (int i = 0; i < arr.size(); i++) {
if (arr[i] < pivot) {
left.push_back(arr[i]);
} else if (arr[i] == pivot) {
equal.push_back(arr[i]);
} else {
right.push_back(arr[i]);
}
}
vector<int> result;
result.insert(result.end(), left.begin(), left.end());
result.insert(result.end(), equal.begin(), equal.end());
result.insert(result.end(), right.begin(), right.end());
return result;
}
int main() {
vector<int> arr = {3, 6, 8, 10, 1, 2, 1};
vector<int> sortedArr = quickSort(arr);
for (int i = 0; i < sortedArr.size(); i++) {
cout << sortedArr[i] << " ";
}
cout << endl;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

return 0;
}

3. Screenshots of Outputs:
1.1

1.2
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

4. Learning Outcomes:
a) Learnt about using optimized sorting techniques for different types of data.
b) Learnt about vectors implementation.
c) Learnt solving problem statements using arrays and vectors.

You might also like