sorting algorithm graphical method

21
SORTING ALGORITHM GRAPHICAL METHOD Made by- Shantanu BCA-V sem. Guided by Mr. Sanjeev Kumar Singh

Upload: shantanu-mishra

Post on 20-Jun-2015

143 views

Category:

Education


3 download

DESCRIPTION

this is the ppt of sorting algorithm

TRANSCRIPT

Page 1: sorting algorithm graphical method

SORTING ALGORITHM GRAPHICAL METHOD

Made by-Shantanu

BCA-V sem.Guided by

Mr. Sanjeev Kumar Singh

Page 2: sorting algorithm graphical method

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.

Page 3: sorting algorithm graphical method

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]

Page 4: sorting algorithm graphical method

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 .

Page 5: sorting algorithm graphical method

BASIC FLOW DIAGRAM

USERSELECTION

OF SORTING ALGORITHM

SORTED ARRAY OUTPUT BY GRAPHICAL

REPRESENTATION

Page 6: sorting algorithm graphical method

SORTING METHODSThe popular sorting methods are:

• Bubble sort• Selection sort• Insertion sort• Merge sort• Quick sort

Page 7: sorting algorithm graphical method

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)

Page 8: sorting algorithm graphical method

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.

Page 9: sorting algorithm graphical method

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

Page 10: sorting algorithm graphical method

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

Page 11: sorting algorithm graphical method

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

Page 12: sorting algorithm graphical method

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;}}

}}

Page 13: sorting algorithm graphical method

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.

Page 14: sorting algorithm graphical method

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

Page 15: sorting algorithm graphical method

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;

}

}

}

Page 16: sorting algorithm graphical method

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

Page 17: sorting algorithm graphical method

ILLUSTRATION OF INSERTION SORTConsider the following array of four items:

90 45 70 15

904570

15

Sorted Array

Unsorted Array

Page 18: sorting algorithm graphical method

After 1st iteration:

After 2nd iteration:

45

90

70

15

Sorted Array

Unsorted Array

45

70

90

15

Sorted Array

Unsorted Array

Page 19: sorting algorithm graphical method

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

Page 20: sorting algorithm graphical method

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;

}

}

Page 21: sorting algorithm graphical method

THANK YOU