0% found this document useful (0 votes)
67 views

Exercise 2 - Array Sorting

The document describes a program to sort an array of integers in ascending order. It includes functions to read input values into an array, print the array, and sort the array using a bubble sort algorithm. The main function calls the other functions to read input, print the initial array, sort the array, and print the sorted array. The program was tested and the output verified for a sample input set.

Uploaded by

Judah Windsor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Exercise 2 - Array Sorting

The document describes a program to sort an array of integers in ascending order. It includes functions to read input values into an array, print the array, and sort the array using a bubble sort algorithm. The main function calls the other functions to read input, print the initial array, sort the array, and print the sorted array. The program was tested and the output verified for a sample input set.

Uploaded by

Judah Windsor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

20CA2013 Data Structures Lab

Exercise number : 2
Date: 22th July, 2021

Array Sorting
• Problem 1 : To sort an array by re-arranging the order of it’s

elements.

• Aim : To sort the elements of an array in ascending order.

• 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

Register number : URK20DA1009


Name : Judah Felix
20CA2013 Data Structures Lab

Function read(int array[10], int n) :

Step 1 : Declare int i


Step 2 : For i = 0; i < n; i++ repeat step 3
Step 3 : Input array[i] as the elements to be entered into the array
Step 4 : End the function

Function print(int array[10], int n) :

Step 1 : Declare int i


Step 2 : For i = 0; i < n; i++ repeat step 3
Step 3 : Display array[i]
Step 4 : End the function

Function sort(int array[10], int n) :

Step 1 : Declare int i, j, temp


Step 2 : For i = 0; i < n; i++ repeat step 3 to step 7
Step 3 : For j = i + 1; j < n; j++ repeat step 4 to step 7
Step 4 : If array[i] > array[j] execute step 5 to step 7
Step 5 : Set temp = array[i]
Step 6 : Set array[i] = array[j]
Step 7 : Set array[j] = temp
Step 8 : For i = 0; i < n; i++ repeat step 9
Step 9 : Display array[i]
Step 10 : End the function

Register number : URK20DA1009


Name : Judah Felix
20CA2013 Data Structures Lab
• Source code :
#include <iostream>
using namespace std;

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';
}

Register number : URK20DA1009


Name : Judah Felix
20CA2013 Data Structures Lab
int main()
{
    int array[10], n, t, position;
    
    cout << "\nEnter the number of elements : ";
    cin >> n;
    
    read(array, n);
    print(array, n);

    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

The array elements are : 45 13 78 34


98 24 90 200 75 77

The elements you entered in sorted order : 13 24


34 45 75 77 78 90 98
200

Press any key to continue . . .

• Result :
The above program was executed and the output was verified
for a sample set of input values.

Register number : URK20DA1009


Name : Judah Felix

You might also like