DSA Lab - Sorting: University of Engineering and Technology

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

University of Engineering and

Technology

DSA Lab -Sorting

Submitted By:

Muhammad Awais
(2019-CS-127)
Submitted To:

Sir Murtaza Qasim


Selection Sorting
Code in C++:
#include <iostream> //header file
void SelectionSort(int arr[], int arrSize)
{
// variable to store the index of the minimum value
int minIndex=INT_MIN;
// Iterate until the N-1 elements
for(int i = 0; i < arrSize - 1; ++i)
{
// Set the first unsorted element
// as the minimum value
minIndex = i;
// Iterate through the unsorted elements only
for(int j = i + 1; j < arrSize; ++j)
{
// set the new minimum value
// if the saved minimum value is higher
// than the current index value
if (arr[j] < arr[minIndex])
minIndex = j;
}
// Swap the the first unsorted element
// with the minimum value
std::swap(arr[i], arr[minIndex]);
}
}
int main()
{
std::cout << "Selection Sort" << std::endl;

// Initialize a new array


int arr[] = {43, 21, 26, 38, 17, 30};
int arrSize = sizeof(arr)/sizeof(*arr);
// Display the initial array
std::cout << "Initial array: ";
for (int i = 0; i < arrSize; ++i)
std::cout << arr[i] << " ";
std::cout << std::endl;
// Sort the array with SelectionSort algorithm
SelectionSort(arr, arrSize);
// Display the sorted array
std::cout << "Sorted array : ";
for (int i = 0; i < arrSize; ++i)
std::cout << arr[i] << " ";
std::cout << std::endl;
return 0;
}
Output

Merge Sorting
Code in C++
#include <iostream>
#include <vector>
std::vector<int> mergeVectors(const std::vector<int>& myVector, const
std::vector<int>& alicesVector)
{
std::vector<int> mergedVector;
if(myVector.size() == 0){
return alicesVector;
}
if(alicesVector.size() == 0){
return myVector;
}
// combine the sorted vectors into one large sorted vector
int i=0, j=0;
//Go through the vectors until there one of iterators hits the end
while(i < myVector.size() && j < alicesVector.size()){
if(myVector[i] <= alicesVector[j]){
mergedVector.push_back(myVector[i]);
i++;
}
else{
mergedVector.push_back(alicesVector[j]);
j++;
}
}
//If myVector is smaller than alicesVector, insert the rest of alicesVector
if(i == myVector.size() && j < alicesVector.size()){
for(j; j<alicesVector.size(); j++){
mergedVector.push_back(alicesVector[j]);
}
}
//If myVector is larger than Alice's vector, insert the rest of myVector
if(i < myVector.size() && j == alicesVector.size()){
for(i; i<myVector.size(); i++){
mergedVector.push_back(myVector[i]);
}
}

return mergedVector;
}
int main() {
std::vector<int> myVector ;
myVector.push_back(2);
myVector.push_back(4);
myVector.push_back(6);
myVector.push_back(8);
std::cout<<"First Sorted array :";
for(int i=0;i<myVector.size();i++)
std::cout<<myVector[i]<<" ";
std::vector<int> alicesVector;
alicesVector.push_back(1);
alicesVector.push_back(7);
std::cout<<"\nSecond Sorted array :";
for(int i=0;i<alicesVector.size();i++)
std::cout<<alicesVector[i]<<" ";
std::vector<int> merged = mergeVectors(myVector, alicesVector);
std::cout<<"\nMerge array :";
for(int i=0; i<merged.size(); i++){
std::cout << merged[i] << " ";
}
std::cout << "\n";
return 0;
}
Output

Quick Sorting
Code in C++
#include <iostream>
#include <stdlib.h>
void Snapshot(int * A, int r) {
std::cout<<"Sorted array :";
for (int i = 0; i < r; i++)
std::cout << A[i] << " ";
}
void exchange(int *A, int S, int E) {
int temp = A[S];
A[S] = A[E];
A[E] = temp;
}
int Partition(int *A, int p, int r) {
for (int j = p; j < r; j++) {
if (A[j] <= A[r]) {
p++;
exchange(A, p - 1, j);
}
}exchange(A, p, r);
return p;
}
int RandomizedPartition(int *A, int p, int r) {
int i = rand() % (r-p+1)+p;
exchange(A, i, r);
return Partition(A, p, r);
}
void QuickSort(int *A, int p, int r) {
if (p < r) {
int q = RandomizedPartition(A, p, r);
QuickSort(A, p, q - 1);
QuickSort(A, q + 1, r);
}
}
int main(int argc, char **argv) {
int arraySize;
std::cout<<"\nEnter the size of array : ";
std::cin >> arraySize;
int* Sequence = new int[arraySize];
std::cout<<"\n\n\tElements in array ";
for (int i = 0; i < arraySize; i++)
{
std::cout<<"\nElement no "<<i+1<<" : ";
std::cin >> Sequence[i];
}
QuickSort(Sequence, 0, arraySize - 1);
Snapshot(Sequence, arraySize); // for printing
delete[] Sequence;
}
Output

You might also like