Exercise 2 - Array Sorting
Exercise 2 - Array Sorting
Exercise number : 2
Date: 22th July, 2021
Array Sorting
• Problem 1 : To sort an array by re-arranging the order of it’s
elements.
• Algorithm :
Step 1 : Start the program
Step 2 : Include iostream
Step 3 : Define a function read - Parameters : int array[10], int n;
Return value : void
Step 4 : Define a function print - Parameters : int array[10], int n;
Return value : void
Step 5 : Define a function sort - Parameters : int array[10], int n;
Return value : void
Step 6 : Inside function main traverse step 7 to step 14
Step 7 : Declare int array[10], n, t, position
Step 8 : Input n as number of array elements
Step 9 : Call function read - Parameters : array, n
Step 10 : Call function print - Parameters : array, n
Step 11 : Call function sort - Parameters : array, n, t
Step 12 : Call function system - Parameters : “pause”
Step 13 : Return 0
Step 14 : End the program
void read(int array[10], int n)
{
int i;
cout << "\nEnter the " << n <<" elements one by one : ";
for(i = 0; i < n; i++)
{
cin >> array[i];
}
}
void print(int array[10], int n)
{
int i;
cout << "\nThe array elements are : ";
for(i = 0; i < n; i++)
cout << array[i] << "\t";
}
void sort(int array[10], int n)
{
int i, j, temp;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
for (i = 0; i < n; i++)
cout << array[i] << '\t';
}
cout << "\n\nThe elements you entered in sorted order : ";
sort(array, n);
cout << "\n\n\n";
system("pause");
return 0;
}
• Output :
Register number : URK20DA1009
Name : Judah Felix
20CA2013 Data Structures Lab
Enter the number of elements : 10
Enter the 10 elements one by one : 45
13
78
34
98
24
90
200
75
77
• Result :
The above program was executed and the output was verified
for a sample set of input values.