SORTING ALGORITHM GRAPHICAL METHOD
Made by-Shantanu
BCA-V sem.Guided by
Mr. Sanjeev Kumar Singh
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.
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]
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 .
BASIC FLOW DIAGRAM
USERSELECTION
OF SORTING ALGORITHM
SORTED ARRAY OUTPUT BY GRAPHICAL
REPRESENTATION
SORTING METHODSThe popular sorting methods are:
• Bubble sort• Selection sort• Insertion sort• Merge sort• Quick sort
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)
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.
ILLUSTRATION OF BUBBLE SORTConsider 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
2nd pass:
3rd pass:
SECOND ITERATION :
1st pass:
2nd pass:
45 70 90 15
45 70 15 90
45 70 15 90
45 70 15 90
45 15 70 90
THIRD ITERATION :
1st pass:
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.
45 15 70 90
15 45 70 90
IMPLIMENTATION IN Cvoid 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;}}
}}
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.
ILLUSTRATION OF SELECTION SORTConsider 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
IMPLIMENTATION IN Cfor(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;
}
}
}
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
ILLUSTRATION OF INSERTION SORTConsider the following array of four items:
90 45 70 15
904570
15
Sorted Array
Unsorted Array
After 1st iteration:
After 2nd iteration:
45
90
70
15
Sorted Array
Unsorted Array
45
70
90
15
Sorted Array
Unsorted Array
After 3rd iteration:
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.
15
45
70
90
Sorted Array
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;
}
}
THANK YOU