Sorting is any process of arranging items systematically, and has two common, yet distinct meanings: ordering: arranging items in a sequence ordered by some criterion; categorizing: grouping items with similar properties.Common sorting algorithms. Sorting algorithm
Bubble/Shell sort: Exchange two adjacent elements if they are out of order. Repeat until array is sorted.
Insertion sort: Scan successive elements for an out-of-order item, then insert the item in the proper place.
Selection sort: Find the smallest (or biggest) element in the array, and put it in the proper place. Swap it with the value in the first position. Repeat until array is sorted.
Quick sort: Partition the array into two segments. In the first segment, all elements are less than or equal to the pivot value. In the second segment, all elements are greater than or equal to the pivot value. Finally, sort the two segments recursively.
'Merge sort': Divide the list of elements in two parts, sort the two parts individually and then merge it.
2. Introduction
• Sorting refers to arranging aset of data in some
logicalorder
• For ex.Atelephone directory canbe considered
asalist where each record hasthree fields -
name, addressand phone number.
• Beingunique, phone number canwork asakeyto
locate anyrecord in the list.
3. Introduction
• Sorting is among the most basic problems in
algorithm design.
• Weare given asequence of items, eachassociated
with agiven key value.And the problem is to
rearrange the items sothat they are inan
increasing(or decreasing) order by key.
• Themethods of sorting canbe divided into two
categories:
• Internal Sorting
• ExternalSorting
4. • Internal Sorting
If all the data that is to be sorted can be adjusted at a
time in main memory, then internal sorting methods
areused
• ExternalSorting
When the data to be sorted can’t be accommodated in
the memory at the sametime and somehasto be kept
in auxiliary memory, then externalsorting methods are
used.
5. Stable and UnStable Sorting
• If asorting algorithm, after sorting the contents,
does notchange the sequence of similar content
in which they appear, it is called stable sorting.
• If asorting algorithm, after sorting the contents,
changesthe sequence of similar content in which
they appear, it is calledunstable sorting.
6. Efficiency of Sorting Algorithm
• Thecomplexity of asorting algorithm measures
the running time ofa function in which n number
of items are to be sorted.
• Thechoice of sorting method depends on
efficiency considerations for different problems.
• Treemost important of these considerationsare:
• Thelength of time spent by programmer in coding a
particular sortingprogram
• Amount of machine time necessaryfor running the
program
• Theamount of memory necessaryfor running the
program
7. Efficiency of Sorting Algorithm
• Various sorting methods are analyzedin the
caseslike – bestcase, worst caseor average
case.
• Most of the sort methods we consider have
requirements thatrange from 0(nlogn) to 0(n2).
• Asort should not be selected only becauseits
sorting time is0(nlogn); the relation of the file
sizen and the other factors affecting the actual
sorting time must beconsidered
8. Efficiency of Sorting Algorithm
• Determining the time requirement of sorting
technique is toactually run the program and
measure itsefficiency.
• Onceaparticular sorting technique is selected the
need is to makethe program asefficient as
possible.
• Anyimprovement in sorting time
significantly affect theoverall efficiency and
savesagreat deal of computer time.
9. Efficiency of Sorting Algorithm
• Spaceconstraints are usually lessimportant than
time considerations.
• Thereason for this canbe, asfor most sorting
programs, the amount of spaceneeded is closer
to 0(n) than to0(n2)
• Thesecond reason is that, if more spaceis
required, it canalmost alwaysbe found in
auxiliary storage.
11. SELECTION SORT
•This sorting algorithm is an in-place
comparison based algorithm .
•In which the list is divided into two parts, the
sorted part at the left end and the unsorted part
at the right end.
• Initially, the sorted part is empty and the
unsorted part is the entire list
12. SELECTION SORT
•The smallest element is selected from the
unsorted array and swapped with the leftmost
element, and that element becomes a part of
the sorted array.
• This process continues moving unsorted array
boundary by one element to the right.
•Each time selecting an item according to its
ordering and placing it in the correct position in
the sequence.
13.
14. Algorithm
Step 1 − Set MIN to location 0 .
Step 2 − Search the minimum element
in the list.
Step 3 − Swap with value at location
MIN .
Step 4 − Increment MIN to point to next
element.
Step 5 − Repeat until list i0s sorted.
15. Complexity
Best case performance О(n^2 )
Average case performance О(n^2 )
Worst case performance О(n^2 )
16. Advantages
The main advantage of the selection
sort is that it performs well on a small
list.
No additional temporary storage is
required.
17. Disadvantages
The primary disadvantage of the
selection sort is its poor efficiency when
dealing with a huge list of items.
The selection sort requires n squared
number of steps for sorting n elements.