Skip to content

Instantly share code, notes, and snippets.

@varshneydevansh
Created November 28, 2017 05:57
Show Gist options
  • Save varshneydevansh/e39e532e4dc5d3ebdd414623da706559 to your computer and use it in GitHub Desktop.
Save varshneydevansh/e39e532e4dc5d3ebdd414623da706559 to your computer and use it in GitHub Desktop.
This is a program of various sorting algorithms using file handling in C++
/*
Implementation of BUBBLE SORT/ SELECTION SORT/ INSERTION SORT Algorithms.
Implemented By - Devansh Varshney CSE3_E E_12
Codechef ID - devansh777
HackerRank/ HackerEarth ID - varshenydevansh
Stack Overflow - varshneydevansh
Git-Hub ID - varshneydevansh
-------------------------------------------------------------------------------------
To use the C++14 install the GCC has to be updated manually.
The following commands show how to add the repository and install the compiler:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-4.9
*/
#include<iostream>
#include<fstream> //for outptut file
#include<stdlib.h> //for exit()
using namespace std;
int bubbleSort()
{
int n,temp;
ofstream my_out("BubbleSort.out"); //Generates the output file
cout << "Enter the no. of elements :\t";
my_out << "Enter the no. of elements :\t" << endl;
cin >> n;
my_out << n << endl;
int a[n];
cout << "\nNow enter the Elements\n";
my_out << "Now enter the Elements" << endl;
for(int a_i = 0 ; a_i <= n-1 ; a_i++)
{
cin >> a[a_i];
my_out << a[a_i] << endl;
}
for(int sort_i = 1 ; sort_i <= n-1 ; sort_i++)
for(int sort_j = 0 ; sort_j < n-sort_i ; sort_j++)
{
if ( a[sort_j] > a[sort_j+1])
{
temp = a[sort_j];
a[sort_j] = a[sort_j+1];
a[sort_j+1] = temp;
}
}
cout << " Sorted Array is - \n";
my_out << " Sorted Array is - \n" << endl;
for(int a_i=0 ; a_i <= n-1 ; a_i++)
{
cout << a[a_i]<<"\t";
my_out << a[a_i] << endl;
}
cout << endl;
return 0;
}
//--------------- Selection sort ---------------------------
int selection()
{
int n,min,temp;
ofstream my_out("Selection_Sort.out"); //Generates the output file
cout << "Enter the No. of elements\n";
my_out << "Enter the No. of elements" << endl;
cin >> n;
my_out << n << endl;
int a[n]; //Array declaration
for(int a_i = 0 ; a_i <= n-1 ; a_i++)
{
cin >> a[a_i];
my_out << a[a_i] << endl;
}
for(int sort_i = 0 ; sort_i < n-1 ; sort_i++)
{
min = sort_i;
for(int sort_j = sort_i+1 ; sort_j <= n-1 ; sort_j++)
{
if ( a[sort_j] < a[min] )
{
min = sort_j;
//Swapping
temp = a[min];
a[min] = a[sort_i];
a[sort_i] = temp;
}
}
}
cout << " Sorted Array is - \n";
my_out << " Sorted Array is - \n" << endl;
for(int a_i=0 ; a_i <= n-1 ; a_i++)
{
cout << a[a_i] << "\t";
my_out << a[a_i] << endl;
}
cout << endl;
return 0;
}
//------------Insertion Sort --------------------------------
int insertion()
{
int n, j, temp;
ofstream my_out("Insertion.out"); //Generates the output file
cout << "Enter the No. of elements\n";
my_out << "Enter the No. of elements" << endl;
cin >> n;
my_out << n << endl;
int a[n]; //Array declaration
my_out << "Enter the Elements" << endl;
for(int a_i = 0 ; a_i <= n-1 ; a_i++)
{
cin >> a[a_i];
my_out << a[a_i] << endl;
}
for(int sort_i = 1 ; sort_i <= n-1 ; sort_i++)
{
temp = a[sort_i];
j = sort_i - 1;
while((temp < a[j]) && (j >= 0))
{
a[j+1] = a[j];
j -= 1;
}
a[j+1] = temp;
}
cout << " Sorted Array is - \n";
my_out << " Sorted Array is - \n" << endl;
for(int a_i=0 ; a_i <= n-1 ; a_i++)
{
cout << a[a_i] << "\t";
my_out << a[a_i] << endl;
}
cout << endl;
return 0;
}
// !!!-----------------------Driver Function ------------------------!!!
int main()
{
int ch;
while(1){
cout <<"\n1. For BubbleSort()\n2. For SelectionSort()\n3. For InsertionSort()\n4. Exit\n\t";
cin >> ch;
switch(ch)
{
case 1: bubbleSort();
break;
case 2: selection();
break;
case 3: insertion();
break;
case 4: exit(0);
}
}
return 0;
}
// All the o/p's are generated in different files.
/*
------------------------------Bubble Sorts o/p-----------------------------------
Enter the no. of elements :
10
Now enter the Elements
12
3
46
21
679
34
24
78
3
77
Sorted Array is -
3
3
12
21
24
34
46
77
78
679
--------------------------------- Selection Sort o/p --------------------------------
Enter the No. of elements
10
23
23
68
325
76
436
12
769
4
768
Sorted Array is -
4
12
23
23
68
76
325
768
436
769
------------------------ Insertion Sorts o/p ---------------------------------------
Enter the No. of elements
10
4
2
6
2
7
1
7
9
2
5
Sorted Array is -
1
2
2
2
4
5
6
7
7
9
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment