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.