53 views

Uploaded by Abhishek kumar singh

save

You are on page 1of 16

TECHNIQUES

By…

Sukanta behera

Reg. No. 07SBSCA048

Topic overview:

NOTES ON SORTING.

** SELECTION SORT ALGORITHM AND
**

EXAMPLE.

** BUBBLE SORT ALGORITHM AND
**

EXAMPLE.

** MERGE SORT ALGORITHM AND
**

EXAMPLE.

** QUICK SORT ALGORITHM AND EXAMPLE.
**

SORTING:

The sorting technique asks us to rearrange

the given items of a given list in ascending

order.

As a practical manner, we usually need to

sort lists of numbers, characters from an

alphabet, character strings, and most

important records in an institutions,

companies etc.

For example, we can choose to sort student

Selection sort:

Selection sort and Bubble sorting techniques are

coming under Brute Force method, which is a

straightforward approach to solving a problem’s

statement and definitions of the concepts involved.

ALGORITHM SelectionSort(A[o..n-1])

//The algorithm sorts a given array by selection sort

//Input: An array A[0..n-1] of orderable elements

//Output: Array A[0..n-1] sorted in ascending order

for i=0 to n-2 do

min=i

for j=i+1 to n-1 do

if A[j] < A[min], min=j

swap A[i] and A[min]

Bubble sort:

Bubble sort is comes under Brute Force method. The

sorting problem is to compare adjacent elements of

the list and exchange them if they are out of order .

By doing it repeatedly, we end up “bubbling up” the

largest element to the last position of the list and vice

versa.

ALGORITHM BubbleSort(A[0..n-1])

//The algorithm sorts array A[0..n-1] by bubblesort

//Input: An array A[0..n-1] of orderable elements

//Output: Array A[0..n-1] sorted in ascending order

for i=0 to n-2 do

for j=0 to n-2-I do

Merge sort:

Merge sort is a perfect example of a successful application of

the Divide and Conquer method. It sorts a given array A[0..n-

1] by dividing it into two halves A[0..[n/2]-1] and A[[n/2]…n-

1]. Sorting each of them recursively and then merging the two

smaller sorted arrays into a single sorted one.

ALGORITHM Mergesort(A[0..n - 1])

//Sorts array A[0..n - 1] by recursive mergesort

//Input: An array A[0..n - 1] of orderable elements

// Output: Array A[0..n - 1] sorted in decreasing order

if n > 1

copy A[0..[n/2] - 1] to B[0…[n/2] - 1]

copy A[[n/2]..n - 1] to C[0…[n/2] - 1]

Mergesort (B[0..[n/2] - 1])

Mergesort (C[0..[n/2] - 1])

Merge (B, C, A)

89 45 68 90 29 34 17

12

89 45 68 90 29 34 17 12

89 68 29 17

45 90 34 12

8 4 6 9 2 3 1 1

9 5 8 0 9 4 7 2

45 68

29 34 12 17

89 90

45 68 89 90 12 17 29 34

12 17 29 34 45 68 89

90

Quick Sort:

Quick sort is another important sorting algorithm that is

based on the Divide and Conquer method. It divides the

input elements according to their value. Specifically, it

rearranges elements of a given array A[0..n – 1] to achieve

its partition.

ALGORITHM Quicksort(A[l…r])

//Sorts a subarray by quicksort

**//Input: A subarray A[l…r] of A[0..n – 1], defined by its left & right
**

indices

//Output: The subarray A[l…r] sorted in nondecreasing order

if l < r

ALGORITHM Partition(A[l…r])

//Partitions a subarray by using its first element as a pivot

//Input: A subarray A[l..r] of A[o…n – 1], defined by its left

and right

// indices l and r (l < r)

//Output: A partition of A[l..r], with the split position returned

as this

// function’s values

p = A[l]

i = l; j = r + 1

repeat

repeat i = i + 1 until A[i] >= p

repeat j = j - 1 until A[j] <= p

Swap(A[i], A[j])

until i >= j

swap(A[i], A[j]) //undo last swap when i >= j

swap(A[l],A[j])

0 1 2 3 4 5 6 7 0 1 2

3 4 5 6 7

5 i3 1 9 8 2 4 j7 1 2 3

4

5 3 1 i9 8 2 j4 7 1

5 3 1 i4 8 2 j9 7 3

ij4

5 3 1 4 i8 j2 9 7 i3

j4

5 3 1 4 j2 i8 9 7

4

Insertion sort:

Insertion sort comes under Decrease and Conquer

method. In this method, we consider an application of

the decrease-by-one technique to sorting an array A[0…

n – 1].

**ALGORITHM Insertionsort(A[0…n – 1])
**

//Sorts a given array by insertion sort

// Input: An array A[0…n – 1] of n orderable elements

// Output: Array A[0…n – 1] sorted in nondecreasing order

for i =1 to n – 1 do

v = A[i]

j=i–1

while j >= 0 and A[j ] > v do

A[j + 1] = A[j]

j=j–1

A[j + 1] = v

- sol2.pdfUploaded byAashish D
- This is My Technical InterviewUploaded byJoy Rahman
- lecture9.pptUploaded byAnonymous niE5VQOH
- Note 6Uploaded byMalathi Sankar
- Data structuresUploaded byAjazKhan
- Bubble Sort AlgorithmUploaded byMouseManX
- Sorting TypesUploaded bykalanithi
- Let Us C SolutionsUploaded byneonav
- data analysis &algorithmUploaded byteju
- Unit 6 FinalbfgUploaded byKrishna Thapa
- SortingUploaded bySiva Kumar
- Fundamentals ofAlgorithmUploaded bylegharimehjabeen
- Array in JavaUploaded byNishant Raj
- DemosLab3Uploaded bySorin Bogdan
- SortingUploaded byDibyanshu Kumar
- ADA_LabUploaded byRajkiran Boppana
- Week12 13 Array 2 - CopyUploaded byEins-Ten Sapta Has Reborn
- gudlecture11Uploaded bysagargupta012
- ijscmc020201Uploaded byijscmc
- adsprograms09[1]Uploaded byGowthami Chebrolu
- Dlib.net Ml GuideUploaded byalok_choudhury_2
- lecture05Uploaded byvv
- SpringMasterClass-CourseGuidev0.1.pdfUploaded byTio Luiz
- Data Structures Viva QuestionsUploaded bySumathi Kanna
- Lecture 09Uploaded byAli Ahmad
- c program.docxUploaded byAbhay Kumar
- EP2256649A1Uploaded bydavid19775891
- Advanced PascalUploaded byMichalis Irakleous
- 1350310292EXCEL - part 2Uploaded byJoel Gwenere

- Sliding Window ProtocolsUploaded byAbhishek kumar singh
- DNS and EmailUploaded byAbhishek kumar singh
- Difference Between Object, Dynamic and Var - CodeProjectUploaded byAbhishek kumar singh
- eBook - Flash MX Level 1Uploaded byAbhishek kumar singh
- HTMLUploaded byAbhishek kumar singh
- Oracle Database 10g - DBAUploaded byAbhishek kumar singh
- Ham 3Uploaded byAbhishek kumar singh
- Wars Hall’s and Floyd’s AlgorithmUploaded byAbhishek kumar singh
- APTLD_DNSUploaded byAbhishek kumar singh
- Portable Ch1Uploaded byAbhishek kumar singh
- Oracle9i Database SummaryUploaded byAbhishek kumar singh
- Construction of Nfa and Dfa From rUploaded byAbhishek kumar singh
- CryptographyUploaded byAbhishek kumar singh
- Data Compression TechniquesUploaded byAbhishek kumar singh