SlideShare a Scribd company logo
DATA STRUCTURE
Chapter 3: Basic Sorting
Algorithms
Prepared & Presented by
Mr. Mahmoud R. Alfarra
2010-2011
College of Science & Technology
Dep. Of Computer Science & IT
BCs of Information Technology
http://mfarra.cst.ps
Out Line
 Introduction
 Bubble Sort Algorithm
 Selection Sort Algorithm
 Insertion Sort Algorithm
2
Introduction
 The two most common operations performed
on data stored in a computer are sorting and
searching.
 This chapter introduces you to the
fundamental algorithms for sorting and
searching data.
 These algorithms depend on only the array as
a data Structure.
3
Sorting Algorithms
 Most of the data we work with in our day-to-
day lives is sorted.
 Sorting is a fundamental process in working
with data and deserves close study.
 Example: We look up a phone
number by moving through the
last names in the book
alphabetically.
4
Bubble Sort Algorithm
5
 The bubble sort is one of the slowest sorting
algorithms available, but it is also one of the
simplest sorts to understand and implement,
which makes it an excellent candidate for our
first sorting algorithm.
 The sort gets its name because values “float
like a bubble” from one end of the list to
another.
Bubble Sort
Algorithm
6
Reference
http://vsmecollege.blogspot.com/2010/05/bubble-sort-bash-shell-programming-with.html
7
Bubble Sort Algorithm
Bubble Sort Algorithm
8
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
1. for ( int pass = 1, pass < length, pass++ )
2. for ( int i = 0; i < length - 1; i++ )
3. if ( b[ i ] > b[ i + 1 ] )
4. Swap(b[ i ], b[ i +1] );
 The outer loop uses to repeat the process of
comparison.
The inner loop compares the two adjacent positions
indicated by i and i +1, swapping them if necessary.
Bubble Sort Algorithm
9
1. static void Main(string[] args)
2. {
3. int[] id = { 12, 30, 1, 4, 7, 2 };
4. Console.Write(" Elements of Array Before Sorting ");
5. for (int x = 0; x < id.Length; x++)
6. Console.Write(" " + id[x]);
7. Console.WriteLine(" ");
8. for (int i =0; i< id.Length; i++)
9. for (int j =0; j< id.Length-1; j++)
10. if (id[j] > id[j + 1])
11. {
12. int hold = id[j];
13. id[j] = id[j + 1];
14. id[j + 1] = hold; }
15. Console.Write(" Elements of Array After Sorting ");
16. for (int x = 0; x < id.Length; x++)
17. Console.Write(" "+id[x]); }
Selection Sort Algorithm
10
 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 Algorithm
11
Selection Sort Algorithm
12
1. static void Main(string[] args)
2. {
3. int[] a = { 10, 2, 34, 4, 3, 1, 100 };
4. int hold;
5. for (int i = 0; i < a.Length; i++) {
6. for (int j = i + 1; j < a.Length; j++)
7. if (a[j] < a[i]) {
8. hold = a[j];
9. a[j] = a[i];
10. a[i] = hold; } }
11. for (int k = 0; k < a.Length; k++)
12. Console.WriteLine(a[k]);
13. Console.Read(); }
Insertion Sort Algorithm
13
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
 The Insertion sort is an analog to the way we
normally sort things numerically or
alphabetically.
 the Insertion sort works not by making
exchanges, but by moving larger array
elements to the right to make room for smaller
elements on the left side of the array.
Insertion Sort Algorithm
14
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
Insertion Sort Algorithm
15
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
‫المصدر‬
:
‫وكيبيديا‬
Insertion Sort Algorithm
16
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
1. static void Main(string[] args) {
2. int[] a = { 10, 2, 34, 4, 3, 1, 100 };
3. int inner, temp;
4. for (int outer = 1; outer < a.Length; outer++)
5. {
6. temp = a[outer];
7. inner = outer;
8. while (inner > 0 && a[inner - 1] >= temp)
9. {
10. a[inner] = a[inner - 1];
11. inner = inner -1; }
12. a[inner] = temp; }
13. Console.WriteLine();
14. for (int k = 0; k < a.Length; k++)
15. Console.WriteLine(" " + a[k]);
16. Console.Read(); }
Time Complexity
17
 Bubble:
 #Compares: O(n2)
 #Swaps: O(n2)
 Selection:
 #Compares: O(n2)
 #Swaps: O(n)
 Insertion
 #Compares: O(n2)
 #Shifts: O(n2)
Thank You …
18
‫البيانات‬ ‫تراكيب‬ ‫مساق‬
‫إعداد‬ ‫العلمية‬ ‫المادة‬
/
‫أ‬
.
‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
Remember that: question is the key of knowledge
Ahl Eljanna 

ِ‫إ‬ ْ
‫م‬ُ
‫ه‬َّ‫ب‬َ
‫ر‬ ‫ا‬ْ
‫و‬َ
‫ق‬َّ‫ات‬ َ
‫ين‬ِ
‫ذ‬َّ‫ل‬‫ا‬ َ
‫يق‬ِ
‫س‬َ
‫و‬
ِ‫إ‬ َّ
‫َّت‬َ
‫ح‬ ‫ا‬ً
‫ر‬َ
‫م‬ُ
‫ز‬ ِ
‫َّة‬‫ن‬َْ
‫ْل‬‫ا‬ َ
‫َل‬
‫ا‬َ
‫وه‬ُ‫اء‬َ
‫ج‬ ‫ا‬َ‫ذ‬
ْ
‫م‬َُ
‫َل‬ َ
‫ال‬َ‫ق‬َ
‫و‬ ‫ا‬َ
‫ه‬ُ‫اب‬َ
‫و‬ْ‫َب‬‫أ‬ ْ
‫ت‬َ
‫ح‬ِ‫ت‬ُ‫ف‬َ
‫و‬
ُ
‫ك‬ْ‫ي‬َ‫ل‬َ
‫ع‬ ٌ
‫م‬ َ
‫َل‬َ
‫س‬ ‫ا‬َ
‫ه‬ُ‫ت‬َ‫ن‬َ
‫ز‬َ
‫خ‬
ْ
‫م‬ُ‫ت‬ْ‫ب‬ِ
‫ط‬ ْ
‫م‬
َ
‫ين‬ِ
‫د‬ِ‫ال‬َ
‫خ‬ ‫ا‬َ
‫وه‬ُ‫ل‬ُ
‫خ‬ْ
‫د‬‫ا‬َ‫ف‬
19

More Related Content

What's hot (20)

3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in Java
Mahmoud Alfarra
 
3 Array operations
3   Array operations3   Array operations
3 Array operations
Mahmoud Alfarra
 
القوائم المترابطة Linked List باستخدام لغة جافا
القوائم المترابطة Linked List باستخدام لغة جافاالقوائم المترابطة Linked List باستخدام لغة جافا
القوائم المترابطة Linked List باستخدام لغة جافا
Mahmoud Alfarra
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
PTCL
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Lovely Professional University
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
Selvin Josy Bai Somu
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
Hanif Durad
 
Linking the prospective and retrospective provenance of scripts
Linking the prospective and retrospective provenance of scriptsLinking the prospective and retrospective provenance of scripts
Linking the prospective and retrospective provenance of scripts
Khalid Belhajjame
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
Hanif Durad
 
Tapp 2014 (belhajjame)
Tapp 2014 (belhajjame)Tapp 2014 (belhajjame)
Tapp 2014 (belhajjame)
Khalid Belhajjame
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Ikc 2015
Ikc 2015Ikc 2015
Ikc 2015
Khalid Belhajjame
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
Queue
QueueQueue
Queue
Budditha Hettige
 
Stack Data structure
Stack Data structureStack Data structure
Stack Data structure
B Liyanage Asanka
 
7 stack and vector
7 stack and vector7 stack and vector
7 stack and vector
Mahmoud Alfarra
 
Control statements
Control statementsControl statements
Control statements
Pramod Rathore
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
faran nawaz
 
3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in Java
Mahmoud Alfarra
 
القوائم المترابطة Linked List باستخدام لغة جافا
القوائم المترابطة Linked List باستخدام لغة جافاالقوائم المترابطة Linked List باستخدام لغة جافا
القوائم المترابطة Linked List باستخدام لغة جافا
Mahmoud Alfarra
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
PTCL
 
Linking the prospective and retrospective provenance of scripts
Linking the prospective and retrospective provenance of scriptsLinking the prospective and retrospective provenance of scripts
Linking the prospective and retrospective provenance of scripts
Khalid Belhajjame
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
nikshaikh786
 
Stack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi LecturerStack and Queue by M.Gomathi Lecturer
Stack and Queue by M.Gomathi Lecturer
gomathi chlm
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Zidny Nafan
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
faran nawaz
 

Similar to Chapter 3: basic sorting algorithms data structure (20)

Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
Micheal Ogundero
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptxDSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
Tribhuvan University
 
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
 
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptxDSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
Data structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithmsData structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithms
Abimbola Idowu
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
Shantanu Mishra
 
Sorting
SortingSorting
Sorting
BHARATH KUMAR
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
Algorithms Analysis
Algorithms Analysis Algorithms Analysis
Algorithms Analysis
Mohammed Hussein
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
Sorting
SortingSorting
Sorting
Shaista Qadir
 
L 14-ct1120
L 14-ct1120L 14-ct1120
L 14-ct1120
Zia Ush Shamszaman
 
Sorting pnk
Sorting pnkSorting pnk
Sorting pnk
pinakspatel
 
UNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptx
UNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptxUNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptx
UNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptx
akashkhedar262
 
Sorting
SortingSorting
Sorting
Kariman Karm Gabaa
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
Micheal Ogundero
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
SajalFayyaz
 
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptxDSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptxDSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
Data structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithmsData structures and algorithms - sorting algorithms
Data structures and algorithms - sorting algorithms
Abimbola Idowu
 
sorting algorithm graphical method
sorting algorithm graphical method sorting algorithm graphical method
sorting algorithm graphical method
Shantanu Mishra
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
UNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptx
UNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptxUNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptx
UNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptx
akashkhedar262
 
Ad

More from Mahmoud Alfarra (20)

Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2
Mahmoud Alfarra
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using Java
Mahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
Mahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
Mahmoud Alfarra
 
Chapter 2: array and array list data structure
Chapter 2: array and array list  data structureChapter 2: array and array list  data structure
Chapter 2: array and array list data structure
Mahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
3 classification
3  classification3  classification
3 classification
Mahmoud Alfarra
 
8 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 201020118 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 20102011
Mahmoud Alfarra
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
Mahmoud Alfarra
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
Mahmoud Alfarra
 
3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 
2 programming-using-java how to built application
2 programming-using-java how to built application2 programming-using-java how to built application
2 programming-using-java how to built application
Mahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
Mahmoud Alfarra
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
Mahmoud Alfarra
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
Mahmoud Alfarra
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
Mahmoud Alfarra
 
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Mahmoud Alfarra
 
Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2
Mahmoud Alfarra
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using Java
Mahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
Mahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
Mahmoud Alfarra
 
Chapter 2: array and array list data structure
Chapter 2: array and array list  data structureChapter 2: array and array list  data structure
Chapter 2: array and array list data structure
Mahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
8 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 201020118 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 20102011
Mahmoud Alfarra
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
Mahmoud Alfarra
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
Mahmoud Alfarra
 
3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 
2 programming-using-java how to built application
2 programming-using-java how to built application2 programming-using-java how to built application
2 programming-using-java how to built application
Mahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
Mahmoud Alfarra
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
Mahmoud Alfarra
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
Mahmoud Alfarra
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
Mahmoud Alfarra
 
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Graph-Based Technique for Extracting Keyphrases In a Single-Document (GTEK)
Mahmoud Alfarra
 
Ad

Recently uploaded (20)

Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
EUPHORIA GENERAL QUIZ FINALS | QUIZ CLUB OF PSGCAS | 21 MARCH 2025
Quiz Club of PSG College of Arts & Science
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
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 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
 
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
Pfeiffer "Secrets to Changing Behavior in Scholarly Communication: A 2025 NIS...
National Information Standards Organization (NISO)
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
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
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
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
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Adam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational PsychologyAdam Grant: Transforming Work Culture Through Organizational Psychology
Adam Grant: Transforming Work Culture Through Organizational Psychology
Prachi Shah
 
Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.Artificial intelligence Presented by JM.
Artificial intelligence Presented by JM.
jmansha170
 
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 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
 
Strengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptxStrengthened Senior High School - Landas Tool Kit.pptx
Strengthened Senior High School - Landas Tool Kit.pptx
SteffMusniQuiballo
 
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
 
Respiratory System , Urinary System
Respiratory  System , Urinary SystemRespiratory  System , Urinary System
Respiratory System , Urinary System
RushiMandali
 
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
 
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKANMATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
MATERI PPT TOPIK 4 LANDASAN FILOSOFIS PENDIDIKAN
aditya23173
 
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17Different pricelists for different shops in odoo Point of Sale in Odoo 17
Different pricelists for different shops in odoo Point of Sale in Odoo 17
Celine George
 
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptxDiptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Diptera: The Two-Winged Wonders, The Fly Squad: Order Diptera.pptx
Arshad Shaikh
 
june 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptxjune 10 2025 ppt for madden on art science is over.pptx
june 10 2025 ppt for madden on art science is over.pptx
roger malina
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdfUnit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Hemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptxHemiptera & Neuroptera: Insect Diversity.pptx
Hemiptera & Neuroptera: Insect Diversity.pptx
Arshad Shaikh
 
Parenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independenceParenting Teens: Supporting Trust, resilience and independence
Parenting Teens: Supporting Trust, resilience and independence
Pooky Knightsmith
 
How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18How to Manage Upselling of Subscriptions in Odoo 18
How to Manage Upselling of Subscriptions in Odoo 18
Celine George
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle SchoolExploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 EmployeeHow to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 

Chapter 3: basic sorting algorithms data structure

  • 1. DATA STRUCTURE Chapter 3: Basic Sorting Algorithms Prepared & Presented by Mr. Mahmoud R. Alfarra 2010-2011 College of Science & Technology Dep. Of Computer Science & IT BCs of Information Technology http://mfarra.cst.ps
  • 2. Out Line  Introduction  Bubble Sort Algorithm  Selection Sort Algorithm  Insertion Sort Algorithm 2
  • 3. Introduction  The two most common operations performed on data stored in a computer are sorting and searching.  This chapter introduces you to the fundamental algorithms for sorting and searching data.  These algorithms depend on only the array as a data Structure. 3
  • 4. Sorting Algorithms  Most of the data we work with in our day-to- day lives is sorted.  Sorting is a fundamental process in working with data and deserves close study.  Example: We look up a phone number by moving through the last names in the book alphabetically. 4
  • 5. Bubble Sort Algorithm 5  The bubble sort is one of the slowest sorting algorithms available, but it is also one of the simplest sorts to understand and implement, which makes it an excellent candidate for our first sorting algorithm.  The sort gets its name because values “float like a bubble” from one end of the list to another.
  • 8. Bubble Sort Algorithm 8 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 1. for ( int pass = 1, pass < length, pass++ ) 2. for ( int i = 0; i < length - 1; i++ ) 3. if ( b[ i ] > b[ i + 1 ] ) 4. Swap(b[ i ], b[ i +1] );  The outer loop uses to repeat the process of comparison. The inner loop compares the two adjacent positions indicated by i and i +1, swapping them if necessary.
  • 9. Bubble Sort Algorithm 9 1. static void Main(string[] args) 2. { 3. int[] id = { 12, 30, 1, 4, 7, 2 }; 4. Console.Write(" Elements of Array Before Sorting "); 5. for (int x = 0; x < id.Length; x++) 6. Console.Write(" " + id[x]); 7. Console.WriteLine(" "); 8. for (int i =0; i< id.Length; i++) 9. for (int j =0; j< id.Length-1; j++) 10. if (id[j] > id[j + 1]) 11. { 12. int hold = id[j]; 13. id[j] = id[j + 1]; 14. id[j + 1] = hold; } 15. Console.Write(" Elements of Array After Sorting "); 16. for (int x = 0; x < id.Length; x++) 17. Console.Write(" "+id[x]); }
  • 10. Selection Sort Algorithm 10  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.
  • 12. Selection Sort Algorithm 12 1. static void Main(string[] args) 2. { 3. int[] a = { 10, 2, 34, 4, 3, 1, 100 }; 4. int hold; 5. for (int i = 0; i < a.Length; i++) { 6. for (int j = i + 1; j < a.Length; j++) 7. if (a[j] < a[i]) { 8. hold = a[j]; 9. a[j] = a[i]; 10. a[i] = hold; } } 11. for (int k = 0; k < a.Length; k++) 12. Console.WriteLine(a[k]); 13. Console.Read(); }
  • 13. Insertion Sort Algorithm 13 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬  The Insertion sort is an analog to the way we normally sort things numerically or alphabetically.  the Insertion sort works not by making exchanges, but by moving larger array elements to the right to make room for smaller elements on the left side of the array.
  • 14. Insertion Sort Algorithm 14 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬
  • 15. Insertion Sort Algorithm 15 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ ‫المصدر‬ : ‫وكيبيديا‬
  • 16. Insertion Sort Algorithm 16 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ 1. static void Main(string[] args) { 2. int[] a = { 10, 2, 34, 4, 3, 1, 100 }; 3. int inner, temp; 4. for (int outer = 1; outer < a.Length; outer++) 5. { 6. temp = a[outer]; 7. inner = outer; 8. while (inner > 0 && a[inner - 1] >= temp) 9. { 10. a[inner] = a[inner - 1]; 11. inner = inner -1; } 12. a[inner] = temp; } 13. Console.WriteLine(); 14. for (int k = 0; k < a.Length; k++) 15. Console.WriteLine(" " + a[k]); 16. Console.Read(); }
  • 17. Time Complexity 17  Bubble:  #Compares: O(n2)  #Swaps: O(n2)  Selection:  #Compares: O(n2)  #Swaps: O(n)  Insertion  #Compares: O(n2)  #Shifts: O(n2)
  • 18. Thank You … 18 ‫البيانات‬ ‫تراكيب‬ ‫مساق‬ ‫إعداد‬ ‫العلمية‬ ‫المادة‬ / ‫أ‬ . ‫ا‬ َّ‫الفــر‬ ‫رفيق‬ ‫محمود‬ Remember that: question is the key of knowledge
  • 19. Ahl Eljanna   ِ‫إ‬ ْ ‫م‬ُ ‫ه‬َّ‫ب‬َ ‫ر‬ ‫ا‬ْ ‫و‬َ ‫ق‬َّ‫ات‬ َ ‫ين‬ِ ‫ذ‬َّ‫ل‬‫ا‬ َ ‫يق‬ِ ‫س‬َ ‫و‬ ِ‫إ‬ َّ ‫َّت‬َ ‫ح‬ ‫ا‬ً ‫ر‬َ ‫م‬ُ ‫ز‬ ِ ‫َّة‬‫ن‬َْ ‫ْل‬‫ا‬ َ ‫َل‬ ‫ا‬َ ‫وه‬ُ‫اء‬َ ‫ج‬ ‫ا‬َ‫ذ‬ ْ ‫م‬َُ ‫َل‬ َ ‫ال‬َ‫ق‬َ ‫و‬ ‫ا‬َ ‫ه‬ُ‫اب‬َ ‫و‬ْ‫َب‬‫أ‬ ْ ‫ت‬َ ‫ح‬ِ‫ت‬ُ‫ف‬َ ‫و‬ ُ ‫ك‬ْ‫ي‬َ‫ل‬َ ‫ع‬ ٌ ‫م‬ َ ‫َل‬َ ‫س‬ ‫ا‬َ ‫ه‬ُ‫ت‬َ‫ن‬َ ‫ز‬َ ‫خ‬ ْ ‫م‬ُ‫ت‬ْ‫ب‬ِ ‫ط‬ ْ ‫م‬ َ ‫ين‬ِ ‫د‬ِ‫ال‬َ ‫خ‬ ‫ا‬َ ‫وه‬ُ‫ل‬ُ ‫خ‬ْ ‫د‬‫ا‬َ‫ف‬ 19