0% found this document useful (0 votes)
7 views5 pages

Nothing

Uploaded by

thaopp.hrt
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)
7 views5 pages

Nothing

Uploaded by

thaopp.hrt
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/ 5

Insertion Sort - Part 1

void insertionSort1(int n, vector<int> arr) {


int value_to_insert = arr[n - 1];
int i = n - 2;

while (i >= 0 && arr[i] > value_to_insert) {


arr[i + 1] = arr[i];
for (int j = 0; j < n; j++) {
cout << arr[j] << " ";
}
cout << endl;
i--;
}

arr[i + 1] = value_to_insert;
for (int j = 0; j < n; j++) {
cout << arr[j] << " ";
}
cout << endl;
}

Insertion Sort - Part 2


void insertionSort2(int n, vector<int> arr) {
for (int i = 1; i < n; i++) {
int value_to_insert = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > value_to_insert) {


arr[j + 1] = arr[j];
j--;
}

arr[j + 1] = value_to_insert;

for (int k = 0; k < n; k++) {


cout << arr[k] << " ";
}
cout << endl;
}

Correctness and the Loop Invariant


void insertionSort(int n, int* arr) {
for (int i = 1; i < n; i++) {
int value_to_insert = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > value_to_insert) {


arr[j + 1] = arr[j];
j--;
}

arr[j + 1] = value_to_insert;
}

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


cout << arr[i] << " ";
}
cout << endl;
}
Running Time of Algorithms
int runningTime(vector<int>& arr) {
int n = arr.size();
int shifts = 0;

for (int i = 1; i < n; i++) {


int value_to_insert = arr[i];
int j = i - 1;

while (j >= 0 && arr[j] > value_to_insert) {


arr[j + 1] = arr[j];
shifts++;
j--;
}
arr[j + 1] = value_to_insert;
}

return shifts;
}
Quicksort 1 - Partition
vector<int> quickSort(vector<int>& arr) {
int pivot = arr[0];
vector<int> left, equal, right;

for (int num : arr) {


if (num < pivot) {
left.push_back(num);
} else if (num == pivot) {
equal.push_back(num);
} else {
right.push_back(num);
}
}

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

Quicksort 2 - Sorting
#include <iostream>
#include <vector>
using namespace std;

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


int pivot = arr[low]; // pivot is the first element in the current sub-array
vector<int> left, right, equal;

for (int i = low + 1; i <= high; i++) {


if (arr[i] < pivot) {
left.push_back(arr[i]);
} else if (arr[i] > pivot) {
right.push_back(arr[i]);
} else {
equal.push_back(arr[i]);
}
}
equal.push_back(pivot);

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

for (int num : result) {


cout << num << " ";
}
cout << endl;

return result;
}

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


if (low < high) {
vector<int> partitioned = quickSortPartition(arr, low, high);

quickSort(partitioned, low, low + partitioned.size() - 1); // Left side


quickSort(partitioned, low + partitioned.size(), high); // Right side
}
}

You might also like