Introduction To Sorting: Practical Considerations For Internal Sorting

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

INTRODUCTION TO SORTING

Sorting means arranging the elements of an array so that they are placed in some relevant order

which may be either ascending or descending. That is, if A is an array, then the elements of A are

arranged in a sorted order (ascending order) in such a way that A[0] < A[1] < A[2] < ...... < A[N].

For example, if we have an array that is declared and initialized as

int A[] = {21, 34, 11, 9, 1, 0, 22};

Then the sorted array (ascending order) can be given as:

A[] = {0, 1, 9, 11, 21, 22, 34;

A sorting algorithm is defined as an algorithm that puts the elements of a list in a certain order,

which can be either numerical order, lexicographical order, or any user-defined order. Efficient

sorting algorithms are widely used to optimize the use of other algorithms like search and merge

algorithms which require sorted lists to work correctly. There are two types of sorting:

 Internal sorting which deals with sorting the data stored in the computer’s memory
 External sorting which deals with sorting the data stored in files. External sorting is applied

when there is voluminous data that cannot be stored in the memory.

Practical Considerations for Internal Sorting

As mentioned above, records can be sorted either in ascending or descending order based on a field

often called as the sort key. The list of records can be either stored in a contiguous and randomly

accessible data structure (array) or may be stored in a dispersed and only sequentially accessible

data structure like a linked list. But irrespective of the underlying data structure used to store the

records, the logic to sort the records will be same and only the implementation details will differ.

When analysing the performance of different sorting algorithms, the practical considerations

would be the following:

 Number of sort key comparisons that will be performed


 Number of times the records in the list will be moved
 Best case performance
 Worst case performance
 Average case performance

BUBBLE SORT
Bubble sort is a very simple method that sorts the array elements by repeatedly moving the
largest element to the highest index position of the array segment (in case of arranging
elements in ascending order). In bubble sorting, consecutive adjacent pairs of elements in the
array are compared with each other. If the element at the lower index is greater than the
element at the higher index, the two elements are interchanged so that the element is placed
before the bigger one. This process will continue till the list of unsorted elements exhausts.
This procedure of sorting is called bubble sorting because elements ‘bubble’ to the top of the
list. Note that at the end of the first pass, the largest element in the list will be placed at its
proper position (i.e., at the end of the list).

Algorithm for bubble sort

Step 1: Repeat Step 2 For 1 = to N-1


Step 2: Repeat For J = to N - I
Step 3: IF A[J] > A[J + 1]
SWAP A[J] and A[J+1]
[END OF INNER LOOP]
[END OF OUTER LOOP]
Step 4: EXIT

Complexity of Bubble Sort


The complexity of any sorting algorithm depends upon the number of comparisons. In bubble
sort, we have seen that there are N–1 passes in total. In the first pass, N–1 comparisons are made to
place the highest element in its correct position. Then, in Pass 2, there are N–2 comparisons and the
second highest element is placed in its position. Therefore, to compute the complexity of bubble
sort, we need to calculate the total number of comparisons. It can be given as:
f(n) = (n – 1) + (n – 2) + (n – 3) + ..... + 3 + 2 + 1
f(n) = n (n – 1)/2
f(n) = n2/2 + O(n) = O(n2)
Therefore, the complexity of bubble sort algorithm is O(n2). It means the time required to execute
bubble sort is proportional to n2, where n is the total number of elements in the array.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy