Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

SORTING

ALGORITHMS
COMPILED BY JN MASI
Sorting

• Sorting according to computing is the process of organizing data


in a certain older so that it can be easily retrieved. Sorting
algorithms are used to arrange data in some older, i.e. from
highest to lowest, alphabetical older etc.
Sorting Algorithm

• A sorting algorithm is a technique for scanning through a


datastructure and rearranging its contents in some specific
order.
Examples of sorting algorithms
• Bubble sort
• Selection sort
• Insertion sort
• Quick sort
• Shell sort
• Heap sort
• Radix sort
• Bucket sort
• Merge sort
Categories of sorting algorithms
Internal Sorting
• Any sort algorithm that uses main memory exclusively during the sorting is
called an internal sort algorithm, this assumes high-speed and random access
to all data members. Internal sorting is faster than external sorting.
External Sorting
• Any sort algorithm that uses external memory, such as tape or disk, during
the sorting

*Find out which sorting algorithms use internal sorting and which algorithms use external
sorting
Sorting concepts
• Sort Stability
• A sorting method is said to be stable if at the end of the method, identical
elements occur in the same relative order as in the original unsorted set.

• Pass
• During the sorted process, the data is traversed many times. Each
traversal of the data is referred to as a sort pass. In addition, the
characteristic of a sort pass is the placement of one or more elements in a
sorted list
Sorting concepts

• Sort older
• Data can be ordered either in ascending order or in descending order. The
order in which the data is organized, either ascending order or descending
order, is called sort order
• Sort efficiency
• Sort efficiency is a measure of the relative efficiency of a sort. It is usually
an estimate of the number of comparisons and data movement required to
Bubble Sort

• Bubble sort works by comparing each element with its


neighbor and swapping them if they are not in the
desired order.
• Its name come from the idea that the larger elements
“Bubble up” to the top or (High end) of the array like
bubbles in a bottle.
Bubble sort Pseudocode
Do
Set swap flag to false
For count = 0 to the next-to-last array subscript
If array[count] is greater than array[count + 1]
Swap the contents of array[count] and array[count + 1]
Set swap flag to true
End If
End For
While the swap flag is true // a swap occurred on the previous pass
Bubble sort in Python
def bubbleSort(myList):
for i in range(len(myList)-1,0,-1):
for j in range (i):
if myList[j]>myList[j + 1]:
temp = myList[j]
myList[j] = myList[j + 1]
myList[j + 1] = temp
return(myList)
Selection sort
• In selection sort, its first iteration selects the smallest element in the list and
swaps it with the first element. The second iteration selects the second-
smallest element (which is the smallest element of the remaining elements)
and swaps it with the second element.
• a selection sort looks for the largest/Smallest value as it makes a pass and,
after completing the pass, places it in the proper location.
• The algorithm continues until the last iteration selects the second-largest
element and swaps it with the second-to-last element, leaving the largest
element in the last index.
Selection sort in python
def selectionSort(myList):
for i in range(len(myList)):
minpos=i
for j in range(i,len(myList)):
if myList[j]<myList[minpos]:
minpos = j
temp = myList[i]
myList[i] = myList[minpos]
myList[minpos] = temp
Insertion sort
• The insertion sort inserts each item into its proper place in the final list.
• The first iteration of this algorithm takes the second element and, if it’s less
than
the first element, swaps it with the first element (i.e., the program inserts the
second element in front of the first element).
• The second iteration looks at the third element and inserts it into the correct
position with respect to the first two elements, so all three elements are in
order. At the nth iteration of this algorithm, the first n elements in the original
array will be sorted.
Insertion sort Example
Insertion sort in Python
def insertionSort(myList):

for index in range (1,len(myList)):

value = myList[index]

i = index - 1

while i >= 0:

if value < myList[i]:

myList[i+1]=myList[i]

myList[i] = value

i = i -1

else:

break
QUESTIONS???

You might also like