Download Experiment 2 Sorting Methods-Bubble, Selection

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Algorithm wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Transcript
Dr. D. Y. Patil Group of Institutions’ Technical Campus
Dr. D. Y. PATIL SCHOOL OF ENGINEERING
Dr. D. Y. Patil Knowledge City, Charoli Bk., Via. Lohegaon, Pune – 412 105
Experiment No: 02
Subject:Data Structures
Title: Sorting Methods-Bubble, Selection &Insertion.
Date Of performance:
Grade:
Class:
Date of Submission:
Signature:
Aim of Experiment:
Implement the following Sort Methods:
1. Bubble sort
2. Insertion sort
3. Selection sort.
Display results after every pass along with no. of comparisons and exchanges for
already sorted input and unsorted data.
Objective :
This assignment is designed to implement different sorting methods. Analyze the
given algorithms.
Theory:
1) Bubble sort:
Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through
the list to be sorted, comparing each pair of adjacent items and swapping them if
they are in the wrong order. The pass through the list is repeated until no swaps are
needed, which indicates that the list is sorted. The algorithm gets its name from the
way smaller elements "bubble" to the top of the list. Because it only uses
comparisons to operate on elements, it is a comparison sort.
Analysis of Bubble Sort:
Bubble sort has best-case complexity Ω (n). When a list is already sorted, bubble
sort will pass through the list once, and find that it does not need to swap any
elements. Thus bubble sort will make only n comparisons and determine that list is
Department of E&TC.
completely sorted. It will also use considerably less time than О (n²) if the elements
in the unsorted list are not too far from their sorted places. The worst case
complexity is O (n2).
Efficiency:
For each pass the number of elements scanned for comparisons reduces by 1.
Number of comparisons in the first pass =(n-1)
Number of comparisons in the Second pass =(n-2)
Number of comparisons in the last pass =1
Thus the total number of comparisons at the end of the algorithm would have been
(n-1) + (n-2) +(n-3)+6+2+1= n(n-1)/2 =n2 /2 +O(n) = O(n2)
Hence the order of the bubble sort algorithm is O(n2) in worst case
Step-by-step example
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number
to greatest number using bubble sort algorithm. In each step, elements written in
bold are being compared.
First Pass:
( 5 1 4 2 8 ) ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and
swaps them.
( 1 5 4 2 8 ) ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5),
algorithm does not swap them.
Second Pass:
(14258)(14258)
( 1 4 2 5 8 ) ( 1 2 4 5 8 ), Swap since 4 > 2
(12458)(12458)
(12458)(12458)
Now, the array is already sorted, but our algorithm does not know if it is completed.
The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
(12458)(12458)
(12458)(12458)
(12458)(12458)
(12458)(12458)
Finally, the array is sorted, and the algorithm can terminate.
Department of E&TC.
2) Selecton sort:
Selection sort works by selecting the smallest unsorted item remaining in the list, and
then swapping it with the item in the next position to be filled. The selection sort has
a complexity of O(n2).
Analysis of Selection Sort:
Selection sort is not difficult to analyze compared to other sorting algorithms since
none of the loops depend on the data in the array. Selecting the lowest element
requires scanning all n elements (this takes n − 1 comparisons) and then swapping it
into the first position. Finding the next lowest element requires scanning the
remaining n − 1 elements and so on, for (n − 1) + (n − 2) + ... + 2 + 1 = n(n − 1) / 2
Θ(n2) comparisons. Each of these scans requires one swap for n − 1 elements (the
final element is already in place).
The algorithm works as follows:
Find the minimum value in the list
Swap it with the value in the first position
Repeat the steps above for the remainder of the list (starting at the second position
and advancing each time)
Here is an example of this sort algorithm sorting five elements:
64 25 12 22 11
11 25 12 22 64
11 12 25 22 64
11 12 22 25 64
11 12 22 25 64
(nothing appears changed on this last line because the last 2 numbers were already
in order)
Department of E&TC.
3) Insertion sort:
Every iteration of insertion sort removes an element from the input data, inserting
it into the correct position in the already-sorted list, until no input elements remain.
The choice of which element to remove from the input is arbitrary, and can be made
using almost any choice algorithm.
Sorting is typically done in-place. The resulting array after k iterations has the
property where the first k + 1 entries are sorted. In each iteration the first remaining
entry of the input is removed, inserted into the result at the correct position, thus
extending the result.
Analysis of Insertion Sort:
The best case input is an array that is already sorted. In this case insertion sort has a
linear running time (i.e., O(n)). During each iteration, the first remaining element of
the input is only compared with the right-most element of the sorted subsection of
the array.
The worst case input is an array sorted in reverse order. In this case every iteration
of the inner loop will scan and shift the entire sorted subsection of the array before
inserting the next element. For this case insertion sort has a quadratic running time
(i.e., O(n2)).
The average case is also quadratic, which makes insertion sort impractical for
sorting large arrays. However, insertion sort is one of the fastest algorithms for
sorting arrays containing fewer than ten elements.
Algorithm:
1. Bubble Sort:
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)
Last =N
2. (Loop)
Repeat through step 5 for Pass= 1,2,6N-1
3. (Initialize Exchanges counter for this Pass)
Department of E&TC.
EXCHS=0
4. Repeat for I=1,2,6.,Last-1
If K[I]>K[I+1]
Then swap (K[I] & K[I+1])
EXCHS=EXCHS+1
5. (Were any exchanges made on this pass?)
If EXCHS==0
Then Return // mission accomplished; return early
Else Last=Last-1
6. (Finished)
Return //maximum number of passes required
2. Selection Sort:
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,6 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)
If MIN_INDEX != PASS
Then swap K[PASS] & K[MIN_INDEX]
5. (Finished)
Return
Department of E&TC.
3. Insertion Sort:
Given array K of N elements, algorithm sorts the elements in increasing (ascending)
order. The variables PASS denotes pass counter &
INDEX value at current pass. I is index of array elements.
1. (Loop)
Repeat through step 4 for PASS=1,2,6 N-1
2. (Initialize)
INDEX=K[PASS]
J=PASS
3. (Obtain the element with smallest value)
Repeat if(J>0) && K[J-1]>INDEX
K[J] =K[J-1]
J=J-1
4. (Assign value at jth location)
K[J]=INDEX
5. (Finished)
Return
Input:
Data in the form an array.
Output:
For each algorithm output should be in the form of:
Original List of numbers.
Sorted list of numbers.
Application:
1) Useful in managing large amount of data.
2) Finding information of a particular student in a large Student Database.
3) Simple Number Guessing game.
Department of E&TC.
FAQ:
What is sorting?
What is recursion?
What is difference between selection, insertion, and bubble sort methods?
What are the applications?
References:
1. ‘C programming’ by Balguruswami.
2. ‘ Let us C’ Kanetkar.
3. ‘Fundamentals of data structures” sartaj sahani.
Conclusion:
Thus we have studied the insertion sort, selection sort and bubble sort algorithms
and analyze the results for best, average and worst cases.
Department of E&TC.