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)

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

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

PPTX
Basic Sorting algorithms csharp
Micheal Ogundero
 
PPTX
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
PPTX
Data structure.pptx
SajalFayyaz
 
PPTX
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
PPTX
Unit vii sorting
Tribhuvan University
 
PPTX
Chapter-2.pptx
selemonGamo
 
PPT
simple-sorting algorithms
Ravirajsinh Chauhan
 
PPTX
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
PDF
Data structures and algorithms - sorting algorithms
Abimbola Idowu
 
PPTX
sorting algorithm graphical method
Shantanu Mishra
 
DOCX
Sorting
BHARATH KUMAR
 
PPTX
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
PDF
Algorithms Analysis
Mohammed Hussein
 
PPT
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
PPT
Sorting
Shaista Qadir
 
PDF
L 14-ct1120
Zia Ush Shamszaman
 
PPTX
Sorting pnk
pinakspatel
 
PPTX
UNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptx
akashkhedar262
 
PDF
Sorting
Kariman Karm Gabaa
 
PPT
Data Structures 6
Dr.Umadevi V
 
Basic Sorting algorithms csharp
Micheal Ogundero
 
Different Sorting tecniques in Data Structure
Tushar Gonawala
 
Data structure.pptx
SajalFayyaz
 
DSA_chapter and chapter 3 _03_Sorting Algorithms.pptx
tahliildhoore54
 
Unit vii sorting
Tribhuvan University
 
Chapter-2.pptx
selemonGamo
 
simple-sorting algorithms
Ravirajsinh Chauhan
 
DSA-sortijejjejjdjjdjdjjsjsjsjsjsjsjng.pptx
suryatom5775
 
Data structures and algorithms - sorting algorithms
Abimbola Idowu
 
sorting algorithm graphical method
Shantanu Mishra
 
Sorting
BHARATH KUMAR
 
Lecture 13 data structures and algorithms
Aakash deep Singhal
 
Algorithms Analysis
Mohammed Hussein
 
358 33 powerpoint-slides_14-sorting_chapter-14
sumitbardhan
 
Sorting
Shaista Qadir
 
L 14-ct1120
Zia Ush Shamszaman
 
Sorting pnk
pinakspatel
 
UNIT I- Sesgfnbghnghnghmghmhgmhmhsion 4.pptx
akashkhedar262
 
Data Structures 6
Dr.Umadevi V
 
Ad

More from Mahmoud Alfarra (20)

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

Recently uploaded (20)

PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Introduction to Indian Writing in English
Trushali Dodiya
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 

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