2. INTRODUCTION
• Sorting of an array is the process of arranging the data present is an
array in a particular manner either by ascending or descending order.
• A sorting algorithm is an algorithm that puts elements of a list in a
certain order.
• The most used orders are numerical order and lexicographical order.
3. WHAT DOES SORTS DO?
I am taking an example:-
Take: [6,24,10,76,35,0,37]
Make the above pattern into this:[0,6,10,24,35,37,76]
4. OBJECTIVE
The main objective of my project is to graphically show sorting
algorithm by arranging the records in ascending order.
The language used in my project is .NET .
5. BASIC FLOW DIAGRAM
USER
SELECTION
OF SORTING
ALGORITHM
SORTED ARRAY
OUTPUT BY
GRAPHICAL
REPRESENTATION
7. HOW FAST CAN WE SORT?
• Selection Sort, Bubble Sort, Insertion Sort :
O(n2)
• Heap Sort, Merge sort :
O(n log n)
• Quicksort :
• Average: O(n log n)
• Best: O(n log n) or O(n)
8. BUBBLE SORT
• Bubble Sort works by comparing each element of the list with the
element next to it and swapping them if required. With each pass, the
largest of the list is "bubbled" to the end of the list whereas the
smaller values sink to the bottom.
9. ILLUSTRATION OF BUBBLE SORT
Consider the following array of four items:
Now I will sort this array using bubble sort:
FIRST ITERATION :
1st pass:
90 45 70 15
90 45 70 15
45 90 70 15
11. THIRD ITERATION :
1st pass:
45 15 70 90
15 45 70 90
As seen from the above illustration , that for four elements, we need
three iterations . It means that if there are n elements then there will
be n-1 iterations.
12. IMPLIMENTATION IN C
void BubbleSort(int a[], int array_size)
{
int i, j, temp;
for (i = 0; i < (array_size - 1); ++i)
{
for (j = 0; j < array_size - 1 - i; ++j )
{
if (a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
}
13. SELECTION SORT
• The idea of Selection Sort is rather simple. It basically determines the
minimum (or maximum) of the list and swaps it with the element at
the index where its supposed to be. The process is repeated such that
the nth minimum (or maximum) element is swapped with the
element at the (n-1)th index of the list.
14. ILLUSTRATION OF SELECTION SORT
Consider the following array of four items:
Now I will sort this array using selection sort:
1st pass:
2nd pass:
3rd pass:
90 45 70 15
15 90 45 70
15 45 90 70
15 45 70 90
15. IMPLIMENTATION IN C
for(i=0;i<=10;i++)
{
for(j=i+1;j<=10;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
16. INSERTION SORT
The Insertion Sort algorithm is a commonly used algorithm. In this type
of sorting algorithm , the array is divided into two parts : the sorted
and unsorted array. The very first element of the array is treated as
sorted array and the rest of array is treated as the unsorted array . For
sorting, first of all the first element of the unsorted array is inserted in
the sorted array. After that the second element is inserted . The
process continues till the last element of the unsorted array is inserted
in the sorted array
17. ILLUSTRATION OF INSERTION SORT
Consider the following array of four items:
90 45 70 15
90
45
70
15
Sorted Array
Unsorted Array
19. After 3rd iteration:
15
45
70
90
Sorted Array
From the above illustration , we see that for an array of four elements ,
we need to process the array three times to get the sorted array. It
means that if there are n elements then there will be n-1 iterations.
20. IMPLIMENTATION IN C
void insertionSort(int a[], int array_size)
{
int i, j, index;
for (i = 1; i < array_size; ++i)
{
index = a[i];
for (j = i; j > 0 && a[j-1] > index; j--)
{
a[j] = a[j-1];
}
a[j] = index;
}
}