Data Structure lab 4

Data Structure lab 4:

Create a program that take an array of 10 inputs from the user and generate the sorted out put using the following Algorithms;

•       Bubble Sort
•       Insertion Sort
•       Selection Sort
Code:
#include <iostream>
using namespace std;
void bubblesort(int c[])
{
                int temp;
                for (int j = 1; j < 10; j++)
                {
                                for (int i = 0; i< 10-j; i++)
                                {
                                                if (c[i]>c[i + 1])
                                                {
                                                                temp = c[i];
                                                                c[i] = c[i + 1];
                                                                c[i + 1] = temp;

                                                }
                                }
                }
                cout << "After Bubble sort: " << endl;
                for (int i = 0; i < 10; i++)
                {
                               
                                cout <<c[i] << endl;
                }
}
void insertionsort(int a[])
{
                int temp;
                for (int i = 1; i <10; i++)
                {
                                for (int j = i; j >= 1; j--)
                                {
                                                if (a[j]<a[j - 1])
                                                {
                                                                temp = a[j];
                                                                a[j] = a[j - 1];
                                                                a[j - 1] = temp;

                                                }
                                }
                }             
                cout << "After Insertion sort: " << endl;
                for (int i = 0; i < 10; i++)
                {

               
                                cout << a[i] << endl;
                }
}
void selectionsort(int b[])
{
                int temp;
                for (int i = 0; i < 10; i++)
                {
                                int min = b[i];
                                int loc = i;
                                for (int j = i + 1; j<10; j++)
                                {
                                                if (min>b[j])
                                                {
                                                                min = b[j];
                                                                loc = j;

                                                }
                                }
                                temp = b[i];
                                b[i] = b[loc];
                                b[loc] = temp;
                }
                cout << "After Selection sort: " << endl;
                for (int i = 0; i < 10; i++)
                {

                               
                                cout << b[i] << endl;
                }
}
int main()
{

                int a[10],b[10],c[10];
                for (int i = 0; i < 10; i++)
                {
                                cout<< "Enter Number " << i + 1 << " ";
                                cin>>a[i];
                                b[i]=a[i];
                                c[i]=a[i];
                }
                bubblesort(a);
                insertionsort(b);
                selectionsort(c);
                system("pause");
                return 0;
}


Comments

Popular posts from this blog

Computer Programming Lab 5

Computer Programming lab 11

Computer Programming Lab 4