SlideShare una empresa de Scribd logo
1 de 19
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

Más contenido relacionado

La actualidad más candente

3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in JavaMahmoud 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 ListPTCL
 
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 scriptsKhalid Belhajjame
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manualnikshaikh786
 
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 Lecturergomathi chlm
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structurefaran nawaz
 

La actualidad más candente (20)

3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in Java
 
3 Array operations
3   Array operations3   Array operations
3 Array operations
 
القوائم المترابطة Linked List باستخدام لغة جافا
القوائم المترابطة Linked List باستخدام لغة جافاالقوائم المترابطة Linked List باستخدام لغة جافا
القوائم المترابطة Linked List باستخدام لغة جافا
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Data structure stack&queue basics
Data structure stack&queue   basicsData structure stack&queue   basics
Data structure stack&queue basics
 
Chapter 11 ds
Chapter 11 dsChapter 11 ds
Chapter 11 ds
 
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
 
Chapter 6 ds
Chapter 6 dsChapter 6 ds
Chapter 6 ds
 
Tapp 2014 (belhajjame)
Tapp 2014 (belhajjame)Tapp 2014 (belhajjame)
Tapp 2014 (belhajjame)
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
Ikc 2015
Ikc 2015Ikc 2015
Ikc 2015
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
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
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Queue
QueueQueue
Queue
 
Stack Data structure
Stack Data structureStack Data structure
Stack Data structure
 
7 stack and vector
7 stack and vector7 stack and vector
7 stack and vector
 
Control statements
Control statementsControl statements
Control statements
 
stacks in algorithems and data structure
stacks in algorithems and data structurestacks in algorithems and data structure
stacks in algorithems and data structure
 

Similar a Chapter 3: basic sorting algorithms data structure

Queue data structure
Queue data structureQueue data structure
Queue data structureMekk Mhmd
 
.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel ProgrammingAlex Moore
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rathSANTOSH RATH
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxingGeetha Manohar
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureRai University
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#Shahzad
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory archana singh
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01Abdul Samee
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queueRajkiran Nadar
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureRai University
 
Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Ameer B. Alaasam
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureRai University
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to AlgorithmsVenkatesh Iyer
 

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

Queue data structure
Queue data structureQueue data structure
Queue data structure
 
.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming
 
Lesson 4 stacks and queues
Lesson 4  stacks and queuesLesson 4  stacks and queues
Lesson 4 stacks and queues
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Autoboxing and unboxing
Autoboxing and unboxingAutoboxing and unboxing
Autoboxing and unboxing
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 
Queue
QueueQueue
Queue
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Unit 3
Unit 3 Unit 3
Unit 3
 
CSE 443 (1).pptx
CSE 443 (1).pptxCSE 443 (1).pptx
CSE 443 (1).pptx
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
 
Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 
Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016)
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
Introduction to Algorithms
Introduction to AlgorithmsIntroduction to Algorithms
Introduction to Algorithms
 
Data structures
Data structuresData structures
Data structures
 

Más de 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 2Mahmoud Alfarra
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using JavaMahmoud Alfarra
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structureMahmoud Alfarra
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structureMahmoud 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 structureMahmoud Alfarra
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structureMahmoud 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 20102011Mahmoud Alfarra
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011Mahmoud 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-tooop20102011Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011Mahmoud 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 computerMahmoud 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 applicationMahmoud Alfarra
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introductionMahmoud 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 2Mahmoud Alfarra
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1Mahmoud 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
 

Más de 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
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using Java
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
 
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
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
 
3 classification
3  classification3  classification
3 classification
 
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
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
 
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
 
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
 
1 programming-using-java -introduction
1 programming-using-java -introduction1 programming-using-java -introduction
1 programming-using-java -introduction
 
تلخيص النصوص تلقائيا
تلخيص النصوص تلقائياتلخيص النصوص تلقائيا
تلخيص النصوص تلقائيا
 
4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز4×4×4 لتحصيل التميز
4×4×4 لتحصيل التميز
 
Data preparation and processing chapter 2
Data preparation and processing chapter  2Data preparation and processing chapter  2
Data preparation and processing chapter 2
 
Introduction to-data-mining chapter 1
Introduction to-data-mining  chapter 1Introduction to-data-mining  chapter 1
Introduction to-data-mining chapter 1
 
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)
 

Último

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Último (20)

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 

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