Vs Code by Me

You might also like

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

#include <iostream>

using namespace std;


void selectionSort(int mainarr[10]){
int mi;
int i;
int arrayLength;

for(int j=0;j<arrayLength;j++){

mi=j;
for(i=j+1;i<arrayLength-1;i++){
if (mainarr[i] < mainarr[mi]){
mi=i;
}

}
int t=mainarr[j];
mainarr[j]=mainarr[mi];
mainarr[mi]=t;
}
for(int i=0;i<arrayLength;i++){
cout<< mainarr[i]<<"\n";
}
}
void bubbleSort(int mainarr[10]){
}
void insertionSort(int mainarr[10]){
}

main() {
//Input//
int x;
//int arrayLength;
int numToSearch,arrayLength,numToAdd;
cout <<"Enter The Length Of Array:";
cin >> arrayLength;
// x=arrayLength;
int mainarr[arrayLength];
cout <<"Enter The Numbers To Add In Array:";
for(int i=0;i<arrayLength;i++){
cin >>mainarr[i];
}
//Choose Sort Algorithm//
cout <<"Press 1 for Selection Sort:2 for Bubble Sort:3 for Insertion Sort:";
int n;
cin >> n;
if(n==1){
selectionSort(mainarr);
}
else if(n==2){
bubbleSort(mainarr);
}
else(n==3);{
insertionSort(mainarr);
}
}
//Program Exit Code (pogram will exit instantly without showing final output
without this code)//
cout<<"Proccess Finished,Press Any Key To Exit!";
int ex;
cin>>ex;
}
//////////////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;

void bubbleSort(int arr[], int n) {


int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

void selectionSort(int arr[], int n) {


int i, j, minIndex;
for (i = 0; i < n-1; i++) {
minIndex = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

void insertSort(int arr[], int n) {


int i, j, key;
for (i = 1; i < n; i++) {
key = arr[i];
j = i-1;
while (j >= 0 && arr[j] > key) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
}

int main() {
int n, i;
cout << "Enter the size of the array: ";
cin >> n;
int arr[n];
cout << "Enter the elements of the array: ";
for (i = 0; i < n; i++) {
cin >> arr[i];
}
bubbleSort(arr, n);
cout << "Array after bubble sort: ";
for (i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
selectionSort(arr, n);
cout << "Array after selection sort: ";
for (i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
insertSort(arr, n);
cout << "Array after insertion sort: ";
for (i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

You might also like