This slide explains three (3) basic sorting algorithms with codes on github. Bubble sort, Selection sort and insertion sort.
visit https://github.com/EngrMikolo/BasicSortingAlgorithms to checkout the codes
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
Basic Sorting algorithms csharp
1. PROGRAMMING IN C#
BASIC SORTING
ALGORITHMS
ENGR. MICHEAL OGUNDERO
YOU CAN CONTACT ME VIA
PHONE - +2348087365099
SKYPE – MICHEAL OGUNDERO
Email – ogunderoayodeji@gmail.com
3. A QUICK ONE! 3
• Write a program that takes a 2-digit integer input from a user.
• The program extracts the two digits from the user input.
• It stores the tens digit in a variable tensDigit and the unit digit in a
variable unitDigit
• It prints tensDigit and unitDigit to the console.
• Also swap the values of the tensDigit and unitDigit
• Print to the console again the current tensDigit and unitDigit
5. INTRODUCTION 5
• The two most common operations performed on data stored in a
computer are sorting and searching.
• Most of the data we work with in our day-to-day lives is sorted. For
example, we find definitions in a dictionary by searching alphabetically.
6. INTRODUCTION 6
• As was mentioned earlier, there has been quite a bit of research performed on
different sorting techniques. Although some very sophisticated sorting
algorithms have been developed.
• There are also several simple sorting algorithms you should study first.
• These sorting algorithms are the sort the bubble sort, selection sort and the
insertion sort. Each of these algorithms is easy to understand and easy to
implement.
• They are not the best overall algorithms for sorting by any means, but for small
data sets and in other special circumstances, they are the best algorithms to use.
7. BIG-O NOTATION 7
• Mathematical logic is the core of all programming. Most of its methods and
concepts are entirely based on what you learned in math class.
• If a computer program is like a meal with several ingredients,
the algorithm is the recipe. It shows you the ingredients (or “inputs”) and
all the steps it will take (or the “procedure”) to put your meal (or “output”)
together.
• Big-O notation is a mathematical function used to describe how complex
an algorithm is — or more specifically, the execution time required by an
algorithm.
• It does this by presenting a “worst-case” scenario of how long it would take
an algorithm to run (or in math terms, it puts an “asymptotic upper bound”
on the time it takes an algorithm to run).
8. BIG-O NOTATION…. 8
• O(1) describes an algorithm that will always execute in the same
time (or space) regardless of the size of the input data set.
• O(N) describes an algorithm whose performance will grow linearly
and in direct proportion to the size of the input data set
• O(N2) represents an algorithm whose performance is directly
proportional to the square of the size of the input data set.
• O(2N) denotes an algorithm whose growth doubles with each
addition to the input data set.
9. BUBBLE SORT 9
The bubble sort is one of the slowest sorting algorithms available, but it is also one
of the simplest sorts to understand and implement.
Assuming you are sorting a list of numbers in ascending order, higher values float
to the right whereas lower values float to the left.
This behavior is caused by moving through the list many times, comparing
adjacent values and swapping them if the value to the left is greater than the
value to the right.
Iteration 1: – 8 5 7 3 1 → 5 8 7 3 1 → 5 7 8 3 1 → 5 7 3 8 1→ 5 7 3 1 8
Iteration 2: – 5 7 3 1 8 → 5 3 7 1 8→ 5 3 1 7 8
Iteration 3: – 5 3 1 7 8 → 3 5 1 7 8 → 3 1 5 7 8
Iteration 4: – 3 1 5 7 8 → 1 3 5 7 8
Iteration 5: – 1 3 5 7 8
11. BUBBLE SORT… 11
In fact, this algorithm is never used in practice but is of historical interest. Like the
brute-force style of searching, it does way too much work to come up with the right
answer!
Time Complexity
Since we need to iterate the entire array for every element, the average and the
worst case complexity of bubble sort is O(n²).
12. SELECTION SORT 12
This algorithm follows the concept of dividing the given array into two subarrays.
• The subarray of sorted elements
• The subarray of unsorted elements
This sort works by starting at the beginning of the array, comparing the first element
with the other elements
in the array.
The smallest element is placed in position 0, and the sort then
begins again at position 1. This continues until each position except the last
position has been the starting point for a new loop.
13. SELECTION SORT 13
Two loops are used in the Selection Sort algorithm.
• The outer loop moves from the first element in the array to the next to last
element, whereas
• the inner loop moves from the second element of the array to the last element,
looking for values that are smaller than the element currently being pointed at by
the outer loop.
• After each iteration of the inner loop, the most minimum value in the array is
assigned to its proper place in the array.
15. SELECTION SORT IMPLEMENTATION 15
Every element needs to be compared to every other element and then get swapped
to its correct position. After every iteration, the size of unsorted array reduces by 1.
Hence n swaps and (n*(n-1))/2 comparisons result in the complexity of Selection
Sort as O(n²).
Let us take an input array as – 8 5 7 3 1
The sorted output for this array is – 1 3 5 7 8
At nth iteration, elements from position 0 to n-1 will be sorted.
16. INSERTION SORT 16
• Here, a sub-list is maintained which is always sorted.
• For example, the lower part of an array is maintained to be sorted. An element
which is to be inserted in this sorted sub-list, has to find its appropriate place and
then it has to be inserted there.
• This algorithm is efficient for smaller datasets.
• The array is searched sequentially and unsorted items are moved and inserted
into the sorted sub-list (in the same array).
17. INSERTION SORT IMPLEMENTATION 17
The Insertion sort has two loops.
• The outer loop moves element by element through the array whereas the inner
loop compares the element chosen in the outer loop to the element next to it in
the array.
• If the element selected by the outer loop is less than the element selected by the
inner loop, array elements are shifted over to the right to make room for the
inner loop element, just as described in the preceding example.
19. CODES 19
Kindly visit my github page for the code
https://github.com/EngrMikolo/BasicSortingAlgorithms
• You can also fork the repo to extend the code