0% found this document useful (0 votes)
2 views10 pages

Ampaty: Seletion Sot

The document contains multiple C++ code snippets demonstrating sorting algorithms, including quicksort with different pivot strategies and a custom sorting method for even and odd indexed elements. It also shows the usage of the standard sort function on a vector. Each code segment is accompanied by example input and output to illustrate the sorting results.

Uploaded by

jitendradlf123
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)
2 views10 pages

Ampaty: Seletion Sot

The document contains multiple C++ code snippets demonstrating sorting algorithms, including quicksort with different pivot strategies and a custom sorting method for even and odd indexed elements. It also shows the usage of the standard sort function on a vector. Each code segment is accompanied by example input and output to illustrate the sorting results.

Uploaded by

jitendradlf123
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/ 10

I 92

pog

Seletion Sot:

int mih int lemp

Bot casC

ampaty

Bk oln) o)

aCjtz tepiy
Date
Page.

Bblble oyt
(intise itabaya; ice)

Spaec
Bel o4)
eaa(j)

uk Sot
Vele quiksortinar vayes, ink skant int
adeate)
nt

iksonaay pictel,d)
itt

patia (inDaret
Tluni int art)

irt)
Void
MoSgot

Mhtevganyti)
itt}

R-TC.=
Q1.)​
b) #include <iostream>
#include <vector>
using namespace std;

int partitionFirstPivot(vector<int>& arr, int low, int high) {


int pivot = arr[low];
int i = low + 1;
int j = high;

while (i <= j) {
while (i <= high && arr[i] <= pivot) i++;
while (j >= low && arr[j] > pivot) j--;
if (i < j) swap(arr[i], arr[j]);
}
swap(arr[low], arr[j]);
return j;
}

int partitionLastPivot(vector<int>& arr, int low, int high) {


int pivot = arr[high];
int i = low - 1;

for (int j = low; j < high; j++) {


if (arr[j] <= pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}

int partitionMiddlePivot(vector<int>& arr, int low, int high) {


int mid = low + (high - low) / 2;
swap(arr[mid], arr[low]); return partitionFirstPivot(arr, low, high);
}

void quickSort(vector<int>& arr, int low, int high, int (*partition)(vector<int>&, int, int)) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1, partition);
quickSort(arr, pivotIndex + 1, high, partition);
}
}

void quickSortFirstPivot(vector<int>& arr) {


quickSort(arr, 0, arr.size() - 1, partitionFirstPivot);
}

void quickSortLastPivot(vector<int>& arr) {


quickSort(arr, 0, arr.size() - 1, partitionLastPivot);
}

void quickSortMiddlePivot(vector<int>& arr) {


quickSort(arr, 0, arr.size() - 1, partitionMiddlePivot);
}

int main() {
vector<int> arr1 = {10, 7, 8, 9, 1, 5};
vector<int> arr2 = arr1;
vector<int> arr3 = arr1;
cout << "Original array: ";
for (int x : arr1) cout << x << " ";
cout << endl;

quickSortFirstPivot(arr1);
cout << "Sorted with first pivot: ";
for (int x : arr1) cout << x << " ";
cout << endl;

quickSortLastPivot(arr2);
cout << "Sorted with last pivot: ";
for (int x : arr2) cout << x << " ";
cout << endl;

quickSortMiddlePivot(arr3);
cout << "Sorted with middle pivot: ";
for (int x : arr3) cout << x << " ";
cout << endl;

return 0;
}

Q2.)
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

class sortarray{
public:
vector<int> sortingresarr(vector<int>& vec){
vector<int> evenplace;
vector<int> oddplace;
vector<int> result;

for(int i=0;i<vec.size();i++){
if(i%2 == 0){
evenplace.push_back(vec[i]);
}
else{
oddplace.push_back(vec[i]);
}
}

sort(evenplace.begin(),evenplace.end());
sort(oddplace.begin(),oddplace.end(),greater<int>());

cout << "Elements in increasing order at even placed index: ";


printarr(evenplace);

cout << "Elements in decreasing order at odd placed index: ";


printarr(oddplace);

result.insert(result.end(),evenplace.begin(),evenplace.end());
result.insert(result.end(),oddplace.begin(),oddplace.end());

return result;
}

void printarr(vector<int> arr){


for(int i=0;i<arr.size();i++){
cout << arr[i] << " ";
}
cout << endl;
}

};
int main(){
sortarray sortarr;

vector<int> arr = {0, 1, 2, 3, 4, 5, 6, 7};

cout << "Input Array: ";

for(int i=0;i<arr.size();i++){
cout << arr[i] << " ";
}
cout << endl;

vector<int> ans = sortarr.sortingresarr(arr);

cout << "Output Array: ";

for(int i=0;i<ans.size();i++){
cout << ans[i] << " ";
}

cout << endl;

return 0;
}

Output:
Q3.)
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

int main(){
vector<int> arr = { 2 ,3 , 8 ,-1 ,7 ,10 };

sort(arr.begin(),arr.end());

for(int res : arr){


cout << res << " ";
}
cout << endl;

return 0;
}
Output:

You might also like