Docsity

Sorting Algorithms: Bubble Sort, Selection Sort, and Insertion Sort, Slides of Object Oriented Programming

Partial preview of the text

Download Sorting Algorithms: Bubble Sort, Selection Sort, and Insertion Sort and more Slides Object Oriented Programming in PDF only on Docsity!

Sorting

Session – 31 to 32

Problem

 (^) Results of VIT entrance exam has been released. Given the details of the students such as name, address and marks scored in entrance, write a program to sort the student details so that it will be convenient to call for counselling.

Sorting also means…

There are several sorting algorithms available like bubble sort, selection sort, insertion sort, quick sort, merge sort, radix sort etc.  (^) Sorting operation is performed in many applications to provide the output in desired order. For example listing all the product in the increasing order of their names or decreasing order of supplier names Searching will be easier in a sorted collection of elements List containing exam scores sorted from lowest to highest or vice versa We study Bubble Sorting, Selection Sorting and Insertion Sorting in this lab course

Compares neighboring pairs of array elements, starting with the last array element, and swaps neighbors whenever they are not in correct order. On each pass, this causes the smallest element to “bubble up” to its correct place in the array.

Bubble Sort

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

Bubble Sort: Pass One

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U N S O R T E D

Bubble Sort: Pass One

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U N S O R T E D

Bubble Sort: Pass One

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U N S O R T E D

Bubble Sort: End Pass One

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U N S O R T E D

SORTED

Bubble Sort: Pass Two

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U N S O R T E D

SORTED

Bubble Sort: Pass Two

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U N S O R T E D

SORTED

Bubble Sort: Pass Three

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U N S O R T E D

SORTED

Bubble Sort: Pass Three

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

U N S O R T E D

SORTED

Bubble Sort: Pass Four

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

UNSORTED

S O R T E D

Bubble Sort: End Pass Four

values [ 0 ] [ 1 ] [ 2 ] [ 3 ] [ 4 ]

S O R T E D