update
This commit is contained in:
parent
2992f4f408
commit
4f46de8d00
3330 changed files with 394553 additions and 76939 deletions
127
.config/Code/User/History/-61386005/07OT.cpp
Normal file
127
.config/Code/User/History/-61386005/07OT.cpp
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int* arr = new int;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> arr[i]){
|
||||
i++;
|
||||
}
|
||||
// infile.close();
|
||||
// return arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr = readData("data_100.txt");
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr[i]<<endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
140
.config/Code/User/History/-61386005/0NvF.cpp
Normal file
140
.config/Code/User/History/-61386005/0NvF.cpp
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int *arr, int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
for(int i=0;i<10000;i++){
|
||||
cout<<arr10000[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
143
.config/Code/User/History/-61386005/15SA.cpp
Normal file
143
.config/Code/User/History/-61386005/15SA.cpp
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr100, 0, 99);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
127
.config/Code/User/History/-61386005/2DTv.cpp
Normal file
127
.config/Code/User/History/-61386005/2DTv.cpp
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
infile.open(fileName);
|
||||
int* arr = new int[100];
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> arr[i]){
|
||||
i++;
|
||||
}
|
||||
// infile.close();
|
||||
// return arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr = readData("data_100.txt");
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr[i]<<endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
132
.config/Code/User/History/-61386005/2RjB.cpp
Normal file
132
.config/Code/User/History/-61386005/2RjB.cpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int arr[], int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
136
.config/Code/User/History/-61386005/2mdN.cpp
Normal file
136
.config/Code/User/History/-61386005/2mdN.cpp
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int arr[], int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
133
.config/Code/User/History/-61386005/4MGS.cpp
Normal file
133
.config/Code/User/History/-61386005/4MGS.cpp
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> dummy) i++;
|
||||
cout<<i<<endl;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
226
.config/Code/User/History/-61386005/4twV.cpp
Normal file
226
.config/Code/User/History/-61386005/4twV.cpp
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout << duration.count() << "ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
selectionSort(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
countTriplets(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
140
.config/Code/User/History/-61386005/61BJ.cpp
Normal file
140
.config/Code/User/History/-61386005/61BJ.cpp
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int *arr, int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr100, 0, 100);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
226
.config/Code/User/History/-61386005/76cb.cpp
Normal file
226
.config/Code/User/History/-61386005/76cb.cpp
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout << duration.count() << "ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
125
.config/Code/User/History/-61386005/9fbl.cpp
Normal file
125
.config/Code/User/History/-61386005/9fbl.cpp
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int* arr = new int;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> dummy) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr = readData("data_100.txt");
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr[i]<<endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
131
.config/Code/User/History/-61386005/9qVn.cpp
Normal file
131
.config/Code/User/History/-61386005/9qVn.cpp
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> dummy) i++;
|
||||
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
|
||||
while(infile >> arr[i]) i++;
|
||||
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr = readData("data_100.txt");
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr[i]<<endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
234
.config/Code/User/History/-61386005/BA4n.cpp
Normal file
234
.config/Code/User/History/-61386005/BA4n.cpp
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout << duration.count() << "ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
selectionSort(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
binarySearch(arr10000, 10000, 8849);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
countTriplets(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
132
.config/Code/User/History/-61386005/Fzw5.cpp
Normal file
132
.config/Code/User/History/-61386005/Fzw5.cpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
207
.config/Code/User/History/-61386005/G420.cpp
Normal file
207
.config/Code/User/History/-61386005/G420.cpp
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
for(int i=0;i<10000;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
for(int i=0;i<10000;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
203
.config/Code/User/History/-61386005/Ipbn.cpp
Normal file
203
.config/Code/User/History/-61386005/Ipbn.cpp
Normal file
|
|
@ -0,0 +1,203 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
132
.config/Code/User/History/-61386005/JDZC.cpp
Normal file
132
.config/Code/User/History/-61386005/JDZC.cpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int arr[], int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = high_resolution_clock::now();
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
208
.config/Code/User/History/-61386005/K1su.cpp
Normal file
208
.config/Code/User/History/-61386005/K1su.cpp
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
auto start1 = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end1 = std::chrono::high_resolution_clock::now();
|
||||
auto duration1 = std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start1);
|
||||
cout<<duration1.count()<<"ms\n";
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
234
.config/Code/User/History/-61386005/KR7m.cpp
Normal file
234
.config/Code/User/History/-61386005/KR7m.cpp
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout << duration.count() << "ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
selectionSort(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
binarySearch(arr10000, 10000, 8849);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
countTriplets(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
128
.config/Code/User/History/-61386005/LdRK.cpp
Normal file
128
.config/Code/User/History/-61386005/LdRK.cpp
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> dummy) i++;
|
||||
cout<<i<<endl;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
208
.config/Code/User/History/-61386005/LpWn.cpp
Normal file
208
.config/Code/User/History/-61386005/LpWn.cpp
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
226
.config/Code/User/History/-61386005/NhV8.cpp
Normal file
226
.config/Code/User/History/-61386005/NhV8.cpp
Normal file
|
|
@ -0,0 +1,226 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout << duration.count() << "ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
selectionSort(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
127
.config/Code/User/History/-61386005/ONri.cpp
Normal file
127
.config/Code/User/History/-61386005/ONri.cpp
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
infile.open(fileName);
|
||||
int* arr = new int;
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> arr[i]){
|
||||
i++;
|
||||
}
|
||||
// infile.close();
|
||||
// return arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
readData("data_100.txt");
|
||||
// for(int i=0;i<100;i++){
|
||||
// cout<<arr[i]<<endl;
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
132
.config/Code/User/History/-61386005/R4uQ.cpp
Normal file
132
.config/Code/User/History/-61386005/R4uQ.cpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int arr[], int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = std::high_resolution_clock::now();
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
132
.config/Code/User/History/-61386005/SleD.cpp
Normal file
132
.config/Code/User/History/-61386005/SleD.cpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int arr[], int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
139
.config/Code/User/History/-61386005/VIk7.cpp
Normal file
139
.config/Code/User/History/-61386005/VIk7.cpp
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int *arr, int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
for(int i=0;i<10000;i++){
|
||||
cout<<arr10000[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
234
.config/Code/User/History/-61386005/XCZx.cpp
Normal file
234
.config/Code/User/History/-61386005/XCZx.cpp
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout << duration.count() << "ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
selectionSort(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
// {
|
||||
// auto start = std::chrono::high_resolution_clock::now();
|
||||
// countTriplets(arr10000, 10000);
|
||||
// auto end = std::chrono::high_resolution_clock::now();
|
||||
// auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
// cout<<duration.count()<<"ms\n";
|
||||
// }
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
binarySearch(arr10000, 10000, 8849);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
143
.config/Code/User/History/-61386005/aKgW.cpp
Normal file
143
.config/Code/User/History/-61386005/aKgW.cpp
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int *arr, int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr100, 0, 99);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
234
.config/Code/User/History/-61386005/aVh4.cpp
Normal file
234
.config/Code/User/History/-61386005/aVh4.cpp
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout << duration.count() << "ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
selectionSort(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
countTriplets(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
binarySearch(arr10000, 10000, 8849);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
136
.config/Code/User/History/-61386005/dEL8.cpp
Normal file
136
.config/Code/User/History/-61386005/dEL8.cpp
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int arr[], int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
1
.config/Code/User/History/-61386005/entries.json
Normal file
1
.config/Code/User/History/-61386005/entries.json
Normal file
|
|
@ -0,0 +1 @@
|
|||
{"version":1,"resource":"file:///home/rafayahmad/Stuff/Coding/C%2B%2B/DS/lab1/lab1.cpp","entries":[{"id":"ho0I.cpp","timestamp":1724670765692},{"id":"ONri.cpp","timestamp":1724670812046},{"id":"sMwS.cpp","timestamp":1724670838133},{"id":"2DTv.cpp","timestamp":1724670851023},{"id":"07OT.cpp","timestamp":1724670938743},{"id":"i2R7.cpp","timestamp":1724670950873},{"id":"9fbl.cpp","timestamp":1724671029426},{"id":"9qVn.cpp","timestamp":1724671057670},{"id":"nFsu.cpp","timestamp":1724671087350},{"id":"zuoC.cpp","timestamp":1724671121970},{"id":"LdRK.cpp","timestamp":1724671211844},{"id":"4MGS.cpp","timestamp":1724671239800},{"id":"thZZ.cpp","timestamp":1724671327107},{"id":"uQsM.cpp","timestamp":1724671416491},{"id":"Fzw5.cpp","timestamp":1724671428231},{"id":"SleD.cpp","timestamp":1724671470008},{"id":"pTP5.cpp","timestamp":1724671484484},{"id":"JDZC.cpp","timestamp":1724671798989},{"id":"R4uQ.cpp","timestamp":1724671824170},{"id":"2RjB.cpp","timestamp":1724671839169},{"id":"p4e1.cpp","timestamp":1724671929619},{"id":"2mdN.cpp","timestamp":1724671954560},{"id":"dEL8.cpp","timestamp":1724672017453},{"id":"s3qj.cpp","timestamp":1724672049907},{"id":"nLW0.cpp","timestamp":1724672065656},{"id":"VIk7.cpp","timestamp":1724672111121},{"id":"0NvF.cpp","timestamp":1724672139776},{"id":"myWg.cpp","timestamp":1724672166114},{"id":"61BJ.cpp","timestamp":1724672182404},{"id":"uXed.cpp","timestamp":1724672249684},{"id":"aKgW.cpp","timestamp":1724672299032},{"id":"15SA.cpp","timestamp":1724672486565},{"id":"vC6U.cpp","timestamp":1724672550376},{"id":"sKZh.cpp","timestamp":1724672669383},{"id":"kFvS.cpp","timestamp":1724672690226},{"id":"G420.cpp","timestamp":1724672723025},{"id":"Ipbn.cpp","timestamp":1724672738308},{"id":"matG.cpp","timestamp":1724672900191},{"id":"LpWn.cpp","timestamp":1724672913057},{"id":"K1su.cpp","timestamp":1724672932907},{"id":"gOtC.cpp","timestamp":1724672956586},{"id":"kzoK.cpp","timestamp":1724673176088},{"id":"76cb.cpp","timestamp":1724673225711},{"id":"NhV8.cpp","timestamp":1724673241131},{"id":"4twV.cpp","timestamp":1724673255207},{"id":"aVh4.cpp","timestamp":1724673306566},{"id":"XCZx.cpp","timestamp":1724673486826},{"id":"KR7m.cpp","timestamp":1724673511516},{"id":"iaCI.cpp","timestamp":1724673616617},{"id":"BA4n.cpp","timestamp":1724673631347}]}
|
||||
208
.config/Code/User/History/-61386005/gOtC.cpp
Normal file
208
.config/Code/User/History/-61386005/gOtC.cpp
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
auto start1 = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end1 = std::chrono::high_resolution_clock::now();
|
||||
auto duration1 = std::chrono::duration_cast<std::chrono::milliseconds>(end1 - start1);
|
||||
cout<<duration1.count()<<"ms\n";
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
127
.config/Code/User/History/-61386005/ho0I.cpp
Normal file
127
.config/Code/User/History/-61386005/ho0I.cpp
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
infile.open(fileName);
|
||||
int* arr = new int;
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> arr[i]){
|
||||
i++;
|
||||
}
|
||||
// infile.close();
|
||||
// return arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
readData("data_100.txt");
|
||||
// for(int i=0;i<100;i++){
|
||||
// cout<<arr[i]<<endl;
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
126
.config/Code/User/History/-61386005/i2R7.cpp
Normal file
126
.config/Code/User/History/-61386005/i2R7.cpp
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int* arr = new int;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> arr[i]){
|
||||
i++;
|
||||
}
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr = readData("data_100.txt");
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr[i]<<endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
234
.config/Code/User/History/-61386005/iaCI.cpp
Normal file
234
.config/Code/User/History/-61386005/iaCI.cpp
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout << duration.count() << "ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
selectionSort(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
binarySearch(arr10000, 10000, 8849);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
countTriplets(arr100, 100);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
207
.config/Code/User/History/-61386005/kFvS.cpp
Normal file
207
.config/Code/User/History/-61386005/kFvS.cpp
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
210
.config/Code/User/History/-61386005/kzoK.cpp
Normal file
210
.config/Code/User/History/-61386005/kzoK.cpp
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout << duration.count() << "ms\n";
|
||||
}
|
||||
|
||||
{
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
208
.config/Code/User/History/-61386005/matG.cpp
Normal file
208
.config/Code/User/History/-61386005/matG.cpp
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 9999);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
findMax(arr10000, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
138
.config/Code/User/History/-61386005/myWg.cpp
Normal file
138
.config/Code/User/History/-61386005/myWg.cpp
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int *arr, int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr100, 0, 100);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
132
.config/Code/User/History/-61386005/nFsu.cpp
Normal file
132
.config/Code/User/History/-61386005/nFsu.cpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> dummy) i++;
|
||||
cout<<i<<endl;
|
||||
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
|
||||
while(infile >> arr[i]) i++;
|
||||
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr = readData("data_100.txt");
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr[i]<<endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
139
.config/Code/User/History/-61386005/nLW0.cpp
Normal file
139
.config/Code/User/History/-61386005/nLW0.cpp
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int arr[], int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
for(int i=0;i<10000;i++){
|
||||
cout<<arr10000[i]<<endl;
|
||||
}
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
135
.config/Code/User/History/-61386005/p4e1.cpp
Normal file
135
.config/Code/User/History/-61386005/p4e1.cpp
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int arr[], int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
132
.config/Code/User/History/-61386005/pTP5.cpp
Normal file
132
.config/Code/User/History/-61386005/pTP5.cpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int arr[], int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
139
.config/Code/User/History/-61386005/s3qj.cpp
Normal file
139
.config/Code/User/History/-61386005/s3qj.cpp
Normal file
|
|
@ -0,0 +1,139 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int arr[], int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr10000, 0, 10000);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
for(int i=0;i<10000;i++){
|
||||
cout<<arr10000[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
207
.config/Code/User/History/-61386005/sKZh.cpp
Normal file
207
.config/Code/User/History/-61386005/sKZh.cpp
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr100, 0, 99);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
127
.config/Code/User/History/-61386005/sMwS.cpp
Normal file
127
.config/Code/User/History/-61386005/sMwS.cpp
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
infile.open(fileName);
|
||||
int* arr = new int[100];
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> arr[i]){
|
||||
i++;
|
||||
}
|
||||
// infile.close();
|
||||
// return arr;
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
readData("data_100.txt");
|
||||
// for(int i=0;i<100;i++){
|
||||
// cout<<arr[i]<<endl;
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
131
.config/Code/User/History/-61386005/thZZ.cpp
Normal file
131
.config/Code/User/History/-61386005/thZZ.cpp
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
132
.config/Code/User/History/-61386005/uQsM.cpp
Normal file
132
.config/Code/User/History/-61386005/uQsM.cpp
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
143
.config/Code/User/History/-61386005/uXed.cpp
Normal file
143
.config/Code/User/History/-61386005/uXed.cpp
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void merge(int *arr, int left, int mid, int right){
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
int leftArr[n1];
|
||||
int rightArr[n2];
|
||||
|
||||
for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int k = left;
|
||||
|
||||
while(i<n1 && j<n2){
|
||||
if(leftArr[i] <= rightArr[j]){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
}
|
||||
else{
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
while(i<n1){
|
||||
arr[k] = leftArr[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
while(j<n2){
|
||||
arr[k] = rightArr[i];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr100, 0, 100);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
207
.config/Code/User/History/-61386005/vC6U.cpp
Normal file
207
.config/Code/User/History/-61386005/vC6U.cpp
Normal file
|
|
@ -0,0 +1,207 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
int findMax(int arr[], int n){
|
||||
int max = arr[0];
|
||||
for(int i=0;i<n;i++){
|
||||
if(arr[i] > max) max = arr[i];
|
||||
}
|
||||
return max;
|
||||
}
|
||||
|
||||
void selectionSort(int arr[], int n){
|
||||
for(int i=0;i<n-1;i++){
|
||||
int minIndex = i;
|
||||
for(int j=i+1;j<n;j++){
|
||||
if(arr[j] < arr[minIndex]){
|
||||
minIndex = j;
|
||||
int temp = arr[i];
|
||||
arr[i] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int countTriplets(int arr[], int n){
|
||||
int count = 0;
|
||||
for(int i=0;i<n;i++){
|
||||
for(int j=i+1;j<n;j++){
|
||||
for(int k=j+1;k<n;k++){
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
// void merge(int *arr, int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
void merge(int* arr, int left, int mid, int right) {
|
||||
int n1 = mid - left + 1;
|
||||
int n2 = right - mid;
|
||||
|
||||
// Create temporary arrays
|
||||
int* L = new int[n1];
|
||||
int* R = new int[n2];
|
||||
|
||||
// Copy data to temporary arrays L[] and R[]
|
||||
for (int i = 0; i < n1; i++)
|
||||
L[i] = arr[left + i];
|
||||
for (int j = 0; j < n2; j++)
|
||||
R[j] = arr[mid + 1 + j];
|
||||
|
||||
// Merge the temporary arrays back into arr[left..right]
|
||||
int i = 0; // Initial index of the first subarray
|
||||
int j = 0; // Initial index of the second subarray
|
||||
int k = left; // Initial index of the merged subarray
|
||||
|
||||
while (i < n1 && j < n2) {
|
||||
if (L[i] <= R[j]) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
} else {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
}
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of L[], if any
|
||||
while (i < n1) {
|
||||
arr[k] = L[i];
|
||||
i++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Copy the remaining elements of R[], if any
|
||||
while (j < n2) {
|
||||
arr[k] = R[j];
|
||||
j++;
|
||||
k++;
|
||||
}
|
||||
|
||||
// Clean up memory
|
||||
delete[] L;
|
||||
delete[] R;
|
||||
}
|
||||
|
||||
// Function to implement merge sort
|
||||
void mergeSort(int* arr, int left, int right) {
|
||||
if (left < right) {
|
||||
// Find the middle point
|
||||
int mid = left + (right - left) / 2;
|
||||
|
||||
// Sort first and second halves
|
||||
mergeSort(arr, left, mid);
|
||||
mergeSort(arr, mid + 1, right);
|
||||
|
||||
// Merge the sorted halves
|
||||
merge(arr, left, mid, right);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int binarySearch(int arr[], int n, int x){
|
||||
int left = 0;
|
||||
int right = n-1;
|
||||
|
||||
while(left <= right){
|
||||
int mid = left + (right-left)/2;
|
||||
if(arr[mid] == x) return mid;
|
||||
else if(arr[mid] < x) left = mid + 1;
|
||||
else right = mid - 1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
while(infile >> dummy) i++;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr100 = readData("data_100.txt");
|
||||
int* arr1000 = readData("data_1000.txt");
|
||||
int* arr5000 = readData("data_5000.txt");
|
||||
int* arr10000 = readData("data_10000.txt");
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
auto start = std::chrono::high_resolution_clock::now();
|
||||
mergeSort(arr100, 0, 99);
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
|
||||
cout<<duration.count()<<"ms\n";
|
||||
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr100[i]<<endl;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
130
.config/Code/User/History/-61386005/zuoC.cpp
Normal file
130
.config/Code/User/History/-61386005/zuoC.cpp
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
// /*
|
||||
// Rafay Ahmad
|
||||
// 23I-2526
|
||||
// */
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
using namespace std;
|
||||
|
||||
// int findMax(int arr[], int n){
|
||||
// int max = arr[0];
|
||||
// for(int i=0;i<n;i++){
|
||||
// if(arr[i] > max) max = arr[i];
|
||||
// }
|
||||
// return max;
|
||||
// }
|
||||
|
||||
// void selectionSort(int arr[], int n){
|
||||
// for(int i=0;i<n-1;i++){
|
||||
// int minIndex = i;
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// if(arr[j] < arr[minIndex]){
|
||||
// minIndex = j;
|
||||
// int temp = arr[i];
|
||||
// arr[i] = arr[minIndex];
|
||||
// arr[minIndex] = temp;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// int countTriplets(int arr[], int n){
|
||||
// int count = 0;
|
||||
// for(int i=0;i<n;i++){
|
||||
// for(int j=i+1;j<n;j++){
|
||||
// for(int k=j+1;k<n;k++){
|
||||
// count++;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return count;
|
||||
// }
|
||||
|
||||
// void merge(int arr[], int left, int mid, int right){
|
||||
// int n1 = mid - left + 1;
|
||||
// int n2 = right - mid;
|
||||
|
||||
// int leftArr[n1];
|
||||
// int rightArr[n2];
|
||||
|
||||
// for(int i=0;i<n1;i++) leftArr[i] = arr[left+i];
|
||||
// for(int i=0;i<n2;i++) rightArr[i] = arr[mid+i+1];
|
||||
|
||||
// int i = 0;
|
||||
// int j = 0;
|
||||
// int k = left;
|
||||
|
||||
// while(i<n1 && j<n2){
|
||||
// if(leftArr[i] <= rightArr[j]){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// }
|
||||
// else{
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// }
|
||||
// k++;
|
||||
// }
|
||||
// while(i<n1){
|
||||
// arr[k] = leftArr[i];
|
||||
// i++;
|
||||
// k++;
|
||||
// }
|
||||
// while(j<n2){
|
||||
// arr[k] = rightArr[i];
|
||||
// j++;
|
||||
// k++;
|
||||
// }
|
||||
// }
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// int binarySearch(int arr[], int n, int x){
|
||||
// int left = 0;
|
||||
// int right = n-1;
|
||||
|
||||
// while(left <= right){
|
||||
// int mid = left + (right-left)/2;
|
||||
// if(arr[mid] == x) return mid;
|
||||
// else if(arr[mid] < x) left = mid + 1;
|
||||
// else right = mid - 1;
|
||||
// }
|
||||
// return -1;
|
||||
// }
|
||||
|
||||
int* readData(string fileName){
|
||||
ifstream infile;
|
||||
int dummy;
|
||||
infile.open(fileName);
|
||||
int i = 0;
|
||||
if(infile.is_open()) cout<<"true\n";
|
||||
while(infile >> dummy) i++;
|
||||
cout<<i<<endl;
|
||||
int* arr = new int[i];
|
||||
i = 0;
|
||||
infile.close();
|
||||
infile.open(fileName);
|
||||
while(infile >> arr[i]) i++;
|
||||
infile.close();
|
||||
return arr;
|
||||
}
|
||||
|
||||
|
||||
int main(){
|
||||
|
||||
int* arr = readData("data_100.txt");
|
||||
for(int i=0;i<100;i++){
|
||||
cout<<arr[i]<<endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue