SlideShare a Scribd company logo
PROGRAMMING IN C#
BASIC SORTING
ALGORITHMS
ENGR. MICHEAL OGUNDERO
YOU CAN CONTACT ME VIA
PHONE - +2348087365099
SKYPE – MICHEAL OGUNDERO
Email – ogunderoayodeji@gmail.com
CONTENT 2
1
2
3
4
INTRODUCTION
BIG-O NOTATION
BUBBLE SORT
5
6
7
8SELECTION SORT
INSERTION SORT
CODES ON GITHUB
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
RESULTS
companyname.com
4
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.
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.
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).
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.
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
BUBBLE SORT IMPLEMENTATION 10
public void BubbleSort() {
int temp;
for(int outer = upper; outer >= 1; outer--) {
for(int inner = 0; inner <= outer-1;inner++) {
if ((int)arr[inner] > arr[inner+1]) {
temp = arr[inner];
arr[inner] = arr[inner+1];
arr[inner+1] = temp;
}
}
this.DisplayElements();
}
}
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²).
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.
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.
SELECTION SORT IMPLEMENTATION 14
public void SelectionSort() {
int min, temp;
for(int outer = 0; outer <= upper; outer++) {
min = outer;
for(int inner = outer + 1; inner <= upper; inner++)
if (arr[inner] < arr[min])
min = inner;
temp = arr[outer];
arr[outer] = arr[min];
arr[min] = temp;
}
}
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.
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).
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.
INSERTION SORT IMPLEMENTATION 18
public void InsertionSort() {
int inner, temp;
for(int outer = 1; outer <= upper; outer++) {
temp = arr[outer];
inner = outer;
while(inner > 0 && arr[inner-1] >= temp) {
arr[inner] = arr[inner-1];
inner -= 1;
}
arr[inner] = temp;
}
}
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
Systems Engineering Department,
University of Lagos, Nigeria.
(234) 808-736-5099 Micheal Ogundero
Thank You

More Related Content

What's hot (20)

Abstraction
AbstractionAbstraction
Abstraction
adil raja
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
Lecture Note-1: Algorithm and Its Properties
Lecture Note-1: Algorithm and Its PropertiesLecture Note-1: Algorithm and Its Properties
Lecture Note-1: Algorithm and Its Properties
Rajesh K Shukla
 
LECTURE 12 WINDOWS FORMS PART 2.pptx
LECTURE 12 WINDOWS FORMS PART 2.pptxLECTURE 12 WINDOWS FORMS PART 2.pptx
LECTURE 12 WINDOWS FORMS PART 2.pptx
AOmaAli
 
Object Oriented Methodology in Java (Lecture-1)
Object Oriented Methodology in Java (Lecture-1)Object Oriented Methodology in Java (Lecture-1)
Object Oriented Methodology in Java (Lecture-1)
Md. Mujahid Islam
 
State space search
State space searchState space search
State space search
chauhankapil
 
Awt
AwtAwt
Awt
Rakesh Patil
 
Advanced Python : Decorators
Advanced Python : DecoratorsAdvanced Python : Decorators
Advanced Python : Decorators
Bhanwar Singh Meena
 
Software Design Concepts
Software Design ConceptsSoftware Design Concepts
Software Design Concepts
Mohammed Fazuluddin
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
baabtra.com - No. 1 supplier of quality freshers
 
Rmi architecture
Rmi architectureRmi architecture
Rmi architecture
Maulik Desai
 
Generations Of Programming Languages
Generations Of Programming LanguagesGenerations Of Programming Languages
Generations Of Programming Languages
sebrown
 
Requirement Analysis
Requirement AnalysisRequirement Analysis
Requirement Analysis
SADEED AMEEN
 
Who Killed The Virtual Case File; Case Analysis
Who Killed The Virtual Case File; Case AnalysisWho Killed The Virtual Case File; Case Analysis
Who Killed The Virtual Case File; Case Analysis
Ahmed Coucha, MBA, MSc
 
Python GUI
Python GUIPython GUI
Python GUI
LusciousLarryDas
 
Incremental and iterative stratergy
Incremental and iterative stratergyIncremental and iterative stratergy
Incremental and iterative stratergy
Sushant Kushwaha
 
Requirement change management
Requirement change managementRequirement change management
Requirement change management
Abdul Basit
 
Data abstraction and object orientation
Data abstraction and object orientationData abstraction and object orientation
Data abstraction and object orientation
Hoang Nguyen
 
Component based development | what, why and how
Component based development | what, why and howComponent based development | what, why and how
Component based development | what, why and how
Rakesh Kumar Jha
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
Lecture Note-1: Algorithm and Its Properties
Lecture Note-1: Algorithm and Its PropertiesLecture Note-1: Algorithm and Its Properties
Lecture Note-1: Algorithm and Its Properties
Rajesh K Shukla
 
LECTURE 12 WINDOWS FORMS PART 2.pptx
LECTURE 12 WINDOWS FORMS PART 2.pptxLECTURE 12 WINDOWS FORMS PART 2.pptx
LECTURE 12 WINDOWS FORMS PART 2.pptx
AOmaAli
 
Object Oriented Methodology in Java (Lecture-1)
Object Oriented Methodology in Java (Lecture-1)Object Oriented Methodology in Java (Lecture-1)
Object Oriented Methodology in Java (Lecture-1)
Md. Mujahid Islam
 
State space search
State space searchState space search
State space search
chauhankapil
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
Muhammad Waqas
 
Generations Of Programming Languages
Generations Of Programming LanguagesGenerations Of Programming Languages
Generations Of Programming Languages
sebrown
 
Requirement Analysis
Requirement AnalysisRequirement Analysis
Requirement Analysis
SADEED AMEEN
 
Who Killed The Virtual Case File; Case Analysis
Who Killed The Virtual Case File; Case AnalysisWho Killed The Virtual Case File; Case Analysis
Who Killed The Virtual Case File; Case Analysis
Ahmed Coucha, MBA, MSc
 
Incremental and iterative stratergy
Incremental and iterative stratergyIncremental and iterative stratergy
Incremental and iterative stratergy
Sushant Kushwaha
 
Requirement change management
Requirement change managementRequirement change management
Requirement change management
Abdul Basit
 
Data abstraction and object orientation
Data abstraction and object orientationData abstraction and object orientation
Data abstraction and object orientation
Hoang Nguyen
 
Component based development | what, why and how
Component based development | what, why and howComponent based development | what, why and how
Component based development | what, why and how
Rakesh Kumar Jha
 

Similar to Basic Sorting algorithms csharp (20)

Chapter-2.pptx
Chapter-2.pptxChapter-2.pptx
Chapter-2.pptx
selemonGamo
 
simple-sorting algorithms
simple-sorting algorithmssimple-sorting algorithms
simple-sorting algorithms
Ravirajsinh Chauhan
 
Sorting
SortingSorting
Sorting
Shaista Qadir
 
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptxDSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
Tribhuvan University
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
Kamal Singh Lodhi
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
Sorting
SortingSorting
Sorting
BHARATH KUMAR
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
Shantanu Mishra
 
Quick sort,bubble sort,heap sort and merge sort
Quick sort,bubble sort,heap sort and merge sortQuick sort,bubble sort,heap sort and merge sort
Quick sort,bubble sort,heap sort and merge sort
abhinavkumar77723
 
9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & Searching
Mandeep Singh
 
Chapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structureChapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structure
Mahmoud Alfarra
 
Chapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptxChapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptx
Dibyesh1
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
Dr. Shaukat Wasi
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
VarchasvaTiwari2
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
CHANDAN KUMAR
 
Sorting method data structure
Sorting method data structureSorting method data structure
Sorting method data structure
sunilchute1
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
Eleonora Ciceri
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
pinakspatel
 
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptxDSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
Ain-ul-Moiz Khawaja
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
Shantanu Mishra
 
Quick sort,bubble sort,heap sort and merge sort
Quick sort,bubble sort,heap sort and merge sortQuick sort,bubble sort,heap sort and merge sort
Quick sort,bubble sort,heap sort and merge sort
abhinavkumar77723
 
9.Sorting & Searching
9.Sorting & Searching9.Sorting & Searching
9.Sorting & Searching
Mandeep Singh
 
Chapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structureChapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structure
Mahmoud Alfarra
 
Chapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptxChapter 8 Sorting in the context of DSA.pptx
Chapter 8 Sorting in the context of DSA.pptx
Dibyesh1
 
Sorting method data structure
Sorting method data structureSorting method data structure
Sorting method data structure
sunilchute1
 
Ad

More from Micheal Ogundero (15)

static methods
static methodsstatic methods
static methods
Micheal Ogundero
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
Micheal Ogundero
 
selection structures
selection structuresselection structures
selection structures
Micheal Ogundero
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
Micheal Ogundero
 
A simple program C# program
A simple program C# programA simple program C# program
A simple program C# program
Micheal Ogundero
 
c# operators
c# operatorsc# operators
c# operators
Micheal Ogundero
 
datatypes_variables_constants
datatypes_variables_constantsdatatypes_variables_constants
datatypes_variables_constants
Micheal Ogundero
 
2 robot types_classifications
2 robot types_classifications2 robot types_classifications
2 robot types_classifications
Micheal Ogundero
 
History of robots
History of robotsHistory of robots
History of robots
Micheal Ogundero
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventions
Micheal Ogundero
 
Dictionary and sets-converted
Dictionary and sets-convertedDictionary and sets-converted
Dictionary and sets-converted
Micheal Ogundero
 
Csharp_List
Csharp_ListCsharp_List
Csharp_List
Micheal Ogundero
 
c# Enumerations
c# Enumerationsc# Enumerations
c# Enumerations
Micheal Ogundero
 
csharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_referencecsharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_reference
Micheal Ogundero
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data Types
Micheal Ogundero
 
csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
Micheal Ogundero
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
Micheal Ogundero
 
A simple program C# program
A simple program C# programA simple program C# program
A simple program C# program
Micheal Ogundero
 
datatypes_variables_constants
datatypes_variables_constantsdatatypes_variables_constants
datatypes_variables_constants
Micheal Ogundero
 
2 robot types_classifications
2 robot types_classifications2 robot types_classifications
2 robot types_classifications
Micheal Ogundero
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventions
Micheal Ogundero
 
Dictionary and sets-converted
Dictionary and sets-convertedDictionary and sets-converted
Dictionary and sets-converted
Micheal Ogundero
 
csharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_referencecsharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_reference
Micheal Ogundero
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data Types
Micheal Ogundero
 
Ad

Recently uploaded (20)

Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive LearningSustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POSHow to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil DisobediencePaper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptxSPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptxRai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Rai dyansty Chach or Brahamn dynasty, History of Dahir History of Sindh NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation SamplerLDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo SlidesOverview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdfThe Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
LDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student NewsLDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student News
LDM & Mia eStudios
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke WarnerPublishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptxROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Final Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptxFinal Sketch Designs for poster production.pptx
Final Sketch Designs for poster production.pptx
bobby205207
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive LearningSustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18How to Configure Vendor Management in Lunch App of Odoo 18
How to Configure Vendor Management in Lunch App of Odoo 18
Celine George
 
How to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POSHow to Manage Inventory Movement in Odoo 18 POS
How to Manage Inventory Movement in Odoo 18 POS
Celine George
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 SlidesHow to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil DisobediencePaper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Allomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdfAllomorps and word formation.pptx - Google Slides.pdf
Allomorps and word formation.pptx - Google Slides.pdf
Abha Pandey
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptxSPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
LDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation SamplerLDMMIA GRAD Student Check-in Orientation Sampler
LDMMIA GRAD Student Check-in Orientation Sampler
LDM & Mia eStudios
 
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition OecdEnergy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
Energy Balances Of Oecd Countries 2011 Iea Statistics 1st Edition Oecd
razelitouali
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptxWhat is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo SlidesOverview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdfThe Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
LDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student NewsLDMMIA Spring Ending Guest Grad Student News
LDMMIA Spring Ending Guest Grad Student News
LDM & Mia eStudios
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke WarnerPublishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
Black and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdfBlack and White Illustrative Group Project Presentation.pdf (1).pdf
Black and White Illustrative Group Project Presentation.pdf (1).pdf
AnnasofiaUrsini
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptxROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdfFEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 

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 – [email protected]
  • 2. CONTENT 2 1 2 3 4 INTRODUCTION BIG-O NOTATION BUBBLE SORT 5 6 7 8SELECTION SORT INSERTION SORT CODES ON GITHUB
  • 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
  • 10. BUBBLE SORT IMPLEMENTATION 10 public void BubbleSort() { int temp; for(int outer = upper; outer >= 1; outer--) { for(int inner = 0; inner <= outer-1;inner++) { if ((int)arr[inner] > arr[inner+1]) { temp = arr[inner]; arr[inner] = arr[inner+1]; arr[inner+1] = temp; } } this.DisplayElements(); } }
  • 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.
  • 14. SELECTION SORT IMPLEMENTATION 14 public void SelectionSort() { int min, temp; for(int outer = 0; outer <= upper; outer++) { min = outer; for(int inner = outer + 1; inner <= upper; inner++) if (arr[inner] < arr[min]) min = inner; temp = arr[outer]; arr[outer] = arr[min]; arr[min] = temp; } }
  • 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.
  • 18. INSERTION SORT IMPLEMENTATION 18 public void InsertionSort() { int inner, temp; for(int outer = 1; outer <= upper; outer++) { temp = arr[outer]; inner = outer; while(inner > 0 && arr[inner-1] >= temp) { arr[inner] = arr[inner-1]; inner -= 1; } arr[inner] = temp; } }
  • 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
  • 20. Systems Engineering Department, University of Lagos, Nigeria. (234) 808-736-5099 Micheal Ogundero Thank You

Editor's Notes

  • #3: Image source:- https://pixabay.com/en/technology-industry-big-data-cpu-3092486/
  • #21: Image source:- https://pixabay.com/en/coding-programming-working-macbook-924920/