0% found this document useful (0 votes)
85 views8 pages

Department: Academic Year: Sem: Subject:: Department of Applied Sciences & Humanities 2020-2021 II)

Here are the answers to the questions: Qu.1 Why Sorting algorithms are important? Sorting algorithms are important because they arrange data in a specific order like ascending or descending order. This makes data retrieval and searching faster and more efficient. Sorting is required for many applications like searching, merging, database operations etc. Qu. 2 What are advantages and disadvantages of Bubble Sort? Advantages: - Simple implementation. Easy to understand. - No additional memory required. In-place sorting algorithm. Disadvantages: - Slow algorithm. Takes O(n^2) time to sort. - Not suitable for large data sets. - Not a stable sorting algorithm.

Uploaded by

Radhika Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views8 pages

Department: Academic Year: Sem: Subject:: Department of Applied Sciences & Humanities 2020-2021 II)

Here are the answers to the questions: Qu.1 Why Sorting algorithms are important? Sorting algorithms are important because they arrange data in a specific order like ascending or descending order. This makes data retrieval and searching faster and more efficient. Sorting is required for many applications like searching, merging, database operations etc. Qu. 2 What are advantages and disadvantages of Bubble Sort? Advantages: - Simple implementation. Easy to understand. - No additional memory required. In-place sorting algorithm. Disadvantages: - Slow algorithm. Takes O(n^2) time to sort. - Not suitable for large data sets. - Not a stable sorting algorithm.

Uploaded by

Radhika Gaikwad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

DIVISION

ROLL NO OF STUDENT

NAME OF STUDENT

ASSIGNMENT NO
10

Write a program to store student information (e.g. Roll No,


Name, Percentage etc.).
a) Display the data in descending order of Percentage
TITLE OF ASSIGNMENT (bubble sort).
b) Display data for roll no specified by user (linear
search).

DATE OF COMPLETION
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

Assignment No. 10

Aim: Write a program to store student information (e.g. Roll No, Name, Percentage etc.).
a) Display the data in descending order of Percentage (bubble sort).
b) Display data for roll no specified by user (linear search).
Objective:
To accept the student information and store it in the structure. Write the c functions to
display the student information according to descending order of percentage and search
the data for given roll no.
Outcomes:
The students will acquainted with bubble sort algorithm and linear search algorithm

Theory :

Sorting Techniques - bubble sort

1. Bubble Sort
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the elements are
swapped if they are not in order. This algorithm is not suitable for large data sets as its
average and worst case complexity are of Ο(n2) where n is the number of items.

How Bubble Sort Works?

We take an unsorted array for our example. Bubble sort takes Ο(n2) time so we're
keeping it short and precise.
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

Bubble sort starts with very first two elements, comparing them to check which one is
greater.

In this case, value 33 is greater than 14, so it is already in sorted locations. Next, we
compare 33 with 27.

We find that 27 is smaller than 33 and these two values must be swapped.

The new array should look like this −

Next we compare 33 and 35. We find that both are in already sorted positions.

Then we move to the next two values, 35 and 10.

We know then that 10 is smaller 35. Hence they are not sorted.
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

We swap these values. We find that we have reached the end of the array. After one
iteration, the array should look like this −

To be precise, we are now showing how an array should look like after each iteration.
After the second iteration, it should look like this −

Notice that after each iteration, at least one value moves at the end.

And when there's no swap required, bubble sorts learns that an array is completely
sorted.

Now we should look into some practical aspects of bubble sort.

Program for descending order Bubble sort:

/* Bubble sort code */

#include <stdio.h>

int main()
{
int array[100], n, c, d, swap;

printf("Enter number of elements\n");


scanf("%d", &n);
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

for (c = 0 ; c < n - 1; c++)


{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] < array[d+1])

{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c < n; c++)


printf("%d\n", array[c]);

return 0;
}

2. Linear Search:

What is Linear Search?

This is the simplest method for searching. In this technique of searching, the element to be
found in searching the elements to be found is searched sequentially in the list/array. This
method can be performed on a sorted or an unsorted list (usually arrays). In case of a
sorted list searching starts from 0th element and continues until the element is found from
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

the list or the element whose value is greater than (assuming the list is sorted in ascending
order), the value being searched is reached.
As against this, searching in case of unsorted list also begins from the 0th element and
continues until the element or the end of the list is reached.

Example:
The list given above is the list of elements in an unsorted array. The array contains ten
elements. Suppose the element to be searched is '46', so 46 is compared with all the
elements starting from the 0th element, and the searching process ends where 46 is found,
or the list ends.
The performance of the linear search can be measured by counting the comparisons done
to find out an element. The number of comparison is 0(n).

Algorithm for Linear Search

It is a simple algorithm that searches for a specific item inside a list. It operates looping on
each element O(n) unless and until a match occurs or the end of the array is reached.

Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

Pseudo code:
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

Step 1: Accept the student information and store it in structure array.


Step 2 : Sort the data using descending order of Percentage and display the information
using bubble sort
Step 3: Display the student data for specified roll no using linear search.
Test Cases:
Input Output
Stud 1 => [1,”xxx”, 78] Output 1:
Stud 2 =>[2, “yyy”,60] Sorted data according to descending order
Stud 3=> [3,”zzz”,80] of percentage
Stud 4=>[4,”ppp”,50] Stud 5=>[5,”qqq”,83]
Stud 5=>[5,”qqq”,83] Stud 3=> [3,”zzz”,80]
Stud 1 => [1,”xxx”, 78]
Stud 2 =>[2, “yyy”,60]
Stud 4=>[4,”ppp”,50]
Output 2:
Enter Roll no - 4
Student data is
Roll no – 4
Name – ppp
Percentage - 50

Conclusion:
The program accepts array of elements and sort it in ascending order.
[Note : Attach the copy of program and output]
Pimpri Chinchwad Education Trust’s

Pimpri Chinchwad College of Engineering, Pune

Department: Department of Applied Sciences & Humanities Academic Year: 2020-2021 Sem : II

Subject: Computer Programming and Problem Solving-II Laboratory (BFE2317)

Questions:

Qu.1 Why Sorting algorithms are important?


Qu. 2 What are advantages and disadvantages of Bubble Sort?

You might also like