Vous êtes sur la page 1sur 5

Data Structures Lab

Assignment No.E-2
Problem Statement:
Write C++ program to store first year percentage of students in array. Write function for
sorting array of floating point numbers in ascending order using
a) Selection Sort
b) Bubble sort and
Prerequisites

Knowledge of different sorting methods.


Learning Objectives
To understand different sorting methods with pros and cons.
To analyze the given algorithms after every passes along with no. of comparisons
Learning Outcome:
After successfully completing this assignment, you should be able to sort numbers using
selection sort and bubble sort.

S/W Packages and Hardware Apparatus Used

Operating System : Latest version of 64 Bit operating systems open source Ubuntu
14.04

Open Source C++ Programming tool G++

SE Computer, Department of Computer Engineering

Data Structures Lab


Theory
1. Bubble Sort

Eg. 14, 6, 20, 3

1
4
6
2
0
3
6
1
4
3
2
0
6
3
1
4
2
0

6
1

4
20
6
31
4

1
4

PASS1

62
30

13
4

2
0

2
0

1
4

1
4

PASS2

PASS3

2
2
0
Like this 0continue till all passes
Given array K of N elements, algorithm sorts the elements in
increasing(ascending) order. The variables Pass & Last denote pass counter &
position of last unsorted element respectively. I is index of array elements and
EXCHs is to count no of exchanges made by any pass.
1. (Initialize)

SE Computer, Department of Computer Engineering

Data Structures Lab


Last =N
2. (Loop)
Repeat through step 5 for Pass= 1,2,N-1
3. (Initialize Exchanges counter for this Pass)
EXCHS=0
4. Repeat for I=1,2,.,Last-1
A. If K[I]>K[I+1]
B. Then
swap (K[I] & K[I+1])
i. EXCHS=EXCHS+1
5. (Were any exchanges made on this pass?)
A. If EXCHS==0
B. Then Return // mission accomplished; return early
C. Else Last=Last-1
6. (Finished)
Return //maximum number of passes required

2. Selection Sort

SE Computer, Department of Computer Engineering

Data Structures Lab

Given array K of N elements, algorithm sorts the elements in


increasing(ascending) order. The variables PASS denotes pass counter &
MIN_INDEX denotes Position of smallest element in that pass. I is index of array
elements.
1. (Loop)
Repeat through step 4 for PASS=1,2, N-1
2. (Initialize)
MIN_INDEX=1
3. (Obtain the element with smallest value)
Repeat for I= PASS+1,PASS+2,...,N
If K[I] <K[MIN_INDEX]
Then MIN_INDEX=I
4. (Exchange Elements)
A. If MIN_INDEX != PASS
B. Then swap K[PASS] & K[MIN_INDEX]
5. (Finished)
Return

SE Computer, Department of Computer Engineering

Data Structures Lab


Input
Input is first year percentage of students in array.

Output
Output in the form of sorted list.
Conclusion
Hence, we have successfully studied concept of bubble sort and selection sort.
Application:
Manly used in manipulating large amount of data. Sometimes used in maintaining file
system.
FAQs :
1.
2.
3.
4.

What is sorting?
What is complexity?
What is complexity of each algorithm?
Analyze the given algorithms after every passes along with no. of comparisons.

SE Computer, Department of Computer Engineering

Vous aimerez peut-être aussi