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

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 twomost 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  Mostof 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.
  • 6.
  • 7.
  • 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.
  • 11.
  • 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