This document discusses sorting algorithms. It explains that sorting refers to arranging data in a specified order like numerical or alphabetical. Selection sort is described as finding the minimum element and swapping it into the correct position in each pass through the array until sorted. An example of selection sort is provided, showing the steps and swaps to sort an array of numbers.
3. Sorting and searching are fundamental
operations in computer science.
Sorting refers to operation of arranging data
in some given order , such as increasing or
decreasing ,with numerical data or with
character data.
Generally, collections of data are presented
in a sorted manner.
4. Let A be a list of n elements A1,A2,……An in
Memory. Sorting A refers to the operation of
rearranging the contents of A so that they
are in increasing in order, so that
A1<A2<A3<…..<An
5. Sorting = ordering.
Sorted = ordered based on a particular way.
Examples of Sorting:
Words in a dictionary are sorted (and case
distinctions are ignored).
Files in a directory are often listed in sorted
order.
The index of a book is sorted (and case
distinctions are ignored).
6. A relatively easy to understand algorithm
Sorts an array in passes
Each pass selects the next smallest element
At the end of the pass, places it where it belongs
Efficiency is O(n2
), hence called a quadratic
sort
Performs:
O(n2
) comparisons
O(n) exchanges (swaps)
7. 7
Idea:
Find the smallest element in the array
Exchange it with the element in the first position
Find the second smallest element and exchange it with
the element in the second position
Continue until the array is sorted
Disadvantage:
Running time depends only slightly on the amount of
order in the file
9. 1. for fill = 0 to n-2 do // steps 2-6 form a
pass
2. set posMin to fill
3. for next = fill+1 to n-1 do
4. if item at next < item at posMin
5. set posMin to next
6. Exchange item at posMin with one at
fill
10. Let A be a list of n numbers. Sorting A refers to the
operation of rearranging the elements of A So they
are in increasing order ,So that,
A[1]<A[2]<A[3]<……….<A[N]
Example :-
List is- 8,4,19,2,7,13,5,16
After Sorting :-
List is-2,4,5,7,8,13,16,19
11. Suppose the following numbers are stored in
an array A:-
53,21,65,27,70,8,99,1.
Pass1-21,53,65,27,70,8,99,1
21,53,65,27,70,8,99,1
21,53,27,65,70,8,99,1
21,53,27,65,70,8,99,1
21,53,27,65,8,70,99,1
21,53,27,65,8,70,99,1
21,53,27,65,8,70,1,99