Вы находитесь на странице: 1из 6

Date :

Program #
Various Sorting Techniques on data of different Array

Aim: To write a C++ program to sort a user –defined list of elements using bubble sort,
insertion sort and selection sort.

Class

array

Data Members

int list[15]
int a

Member Functions

accept()
bsort()
ssort()
isort()
display()
swap()
Algorithm

1. Start the process.

2. Declare variable ‘arr’ of type ‘array’.
3. Display the menu of actions that can be taken by the user.
4. If the user’s choice is to enter the list of elements, invoke the ‘arr.accept ()’ function.
i. Assign the value of ‘a’ to –1.
ii. Repeat step iii while (list[a]!= -1). Then decrement the value of ‘a’.
iii. Increment the value of ‘a’ and then accept ‘list[a]’.
5. Else if the user’s choice is to sort the elements using bubble sort, invoke the
‘arr.bsort()’ function.
i. Declare integer variable‘t’ and assign to it the value in ‘a’.
ii. for (int n=o; n<=t; n++), repeat steps ‘a’ and ‘b’.
a. for (int x=0; x<t; x++), repeat step ‘b’.
b. Check if (list[x] > list[x+1]). Then interchange the values in
‘list[x]’ and ‘list[x+1]’.
iii. Display the list of elements..
6. Else if the user’s choice is to sort the list using selection sort, invoke the ‘arr.ssort ()’
function.
i. Declare integer variable ‘t’ and assign to it the value in ‘a’. Also declare integer
variable ‘m’ and assign to it 0.
ii. for (inti=0; i<t; i++), repeat steps ‘a’, ‘b’, ‘c’ and ‘d’.
a. Assign m = i.
b. for (int = k+1; k<=t; k++) repeat step ‘c’.
c. Check if (list[k] <list[m]). Then assign m = k.
d. Interchange the values in list[m] and list[i].
iii. Display the list of elements.
7. Else if the user’s choice is to sort the list using insertion sort, invoke the ‘arr.isort ()’
function.
i. Declare integer variable ‘t’ and assign to it the value in ‘a’. Also declare integer
variable ‘m’.
ii. for (inti=0; i<t; i++), repeat steps ‘a’, ‘b’ and ‘c’.
a. Check if (list[i+1] <list[i]). Then execute steps ‘b’ and ‘c’.
b. Assign m = list[i+1].
c. for (int x = i+1; list [x-1] >m; x--), interchange the values in
list[x] and list[x-1].
iii. Display the list of elements.
8. End the process.
#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<dos.h>
class array
{
int list[15],a;
public:

void accept()
{
clrscr();
a=-1;
gotoxy(25,2);
cout<<"ARRAY ENTRY\n\n";
cout<<"Please enter the elements. Enter -1 to stop.\n";
do
{
a++;
cin>>list[a];
}while(list[a]!=(-1));
a--;
}
voidbsort()
{
int t=a;
for(int n=0; n<=t; n++) //Outer loop to perform multiple passes
{
for(int x=0; x<t; x++) //Inner loop to carry out one pass
{
if(list[x]>list[x+1])//If succeeding element is greater, swap
swap(list[x], list[x+1]);
}
}
display();
}
voidssort()
{
int t=a;
int m=0;
for(inti=0; i<t; i++) //Outer loop -multiple passes
{
m=i; //Index of first element in the sorted portion -reference value
for(int k=i+1; k<=t; k++)//Inner loop -traversing the array
{
if(list[k]<list[m])//Comparison with reference
m=k; //New largest value
}
swap(list[m],list[i]); //Swap last in sorted part with new largest

}
display();
}
voidisort()
{
int t=a;
int m;
for(inti=0; i<t; i++) //Outer loop -multiple passes
{
if(list[i+1]<list[i]) //Check if succeeding element is larger
{
m=list[i+1]; //Larger element is picked up
for(int x=i+1;list[x-1]>m; x--) //Loop to find the place for
element
swap(list[x], list[x-1]);
}

}
display();
}
void display()
{
clrscr();
gotoxy(25,2);
cout<<"ARRAY DISPLAY\n\n";
for(int z=0;z<=a;z++)
cout<<list[z]<<endl;
delay(5000);
}
void swap(int&w, int&u)
{
int temp=w;
w=u;
u=temp;
}
}arr;
void main()
{
}
{
clrscr();
intch;
gotoxy(25,2);
cout<<"ARRAY SEARCH\n\n";
cout<<"1. Enter the list.\n\n2. Sort the list using bubble sort.\n\n";
cout<<"3. Sort the list using selection sort.\n\n4. Sort the list using insertion
sort.\n\n";
cin>>ch;
switch(ch)
{
case 1:arr.accept();
break;
case 2:arr.bsort();
break;
case 3:arr.ssort();
break;
case 4:arr.isort();
break;
case 5:exit(0);
}
}
ARRAY SORT

5. Exit