0% found this document useful (0 votes)
3 views9 pages

Sorting Algorithms

The document discusses sorting algorithms, including internal and external sorting techniques, with a focus on bubble sort and selection sort. It covers their efficiencies, advantages, and limitations, highlighting that both algorithms have a time complexity of O(n^2). Additionally, the document includes examples of problems related to arrays, such as finding duplicates and missing numbers.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views9 pages

Sorting Algorithms

The document discusses sorting algorithms, including internal and external sorting techniques, with a focus on bubble sort and selection sort. It covers their efficiencies, advantages, and limitations, highlighting that both algorithms have a time complexity of O(n^2). Additionally, the document includes examples of problems related to arrays, such as finding duplicates and missing numbers.

Uploaded by

Alessandro
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

Week 8

Sorting Algorithms

Introduction

• Sorting and searching are two of the most common operations performed by computers all around
the world.

• The sorting operation arranges the numerical and alphabetical data present in a list, in a specific
order or sequence.

• Searching, on the other hand, locates a specific element across a given list of elements.

• At times, a list may require sorting before the search operation can be performed on it.

Sorting Techniques

• Internal Sorting

• External Sorting

Internal Sorting

• All sorting techniques which require the data set to be present in the main memory are referred as
internal sorting techniques.

Examples

• Selection sort

• Bubble sort

• Insertion sort

• Shell sort

• Quick sort

• Heap sort

External Sorting

• External Sorting, takes place in the secondary memory of a computer.

• Since the number of objects to be sorted is too large to fit in main memory.

Examples

• Merge Sort

• Multiway Merge

• Polyphase merge
Bubble Sort

• Bubble sort is one of the simplest internal sorting algorithms. Bubble sort works by comparing two

consecutive elements and the largest element among these two bubbles towards right.

• At the end of the first pass the largest element gets sorted and placed at the end of the sorted list.

• This process is repeated for all pairs of the elements until it moves the largest element to the end
of the list in that iteration.

• Bubble sort consists of n-1 passes, where ‘n’ is the number of elements to be sorted.

• In the 1st pass, the largest element will be placed in the nth position.

• In the 2nd pass, the second largest element will be placed in the (n-1)th position.

• In (n-1)th pass only the first two elements are compared.


Efficiency of Bubble Sort

• Assume that an array containing n elements is sorted using bubble sort technique.

• Number of comparisons made in first pass = n–1.

• Number of comparisons made in second pass = n – 2.

• Number of comparisons made in last pass = 1.

• Total number of comparisons made = (n – 1) + (n – 2) + . . . + 1

= n * (n – 1) / 2

= O(n2)

• Thus, efficiency of bubble sort = O(n2).

Analysis of Bubble Sort

• Best case analysis : O(n2)

• Average case analysis : O(n2)

• Worst case analysis : O(n2)

Advantages of Bubble Sort

• It is easy to understand and implement.

• It leverages the presence of any existing sort pattern in the list, thus resulting in better

efficiency.
Limitations of Bubble Sort

• The efficiency of O(n2) is not well suited for large sized lists.

• It requires large number of elements to be shifted.

• It is slow in execution as large elements are moved towards the end of the list in a step-by-step
fashion.

Selection Sort

• Selection sort is one of the most basic sorting techniques.

• It works on the principle of identifying the smallest element in the list and moving it to the
beginning of the list.

• This process is repeated until all the elements in the list are sorted.

Example

• Let us consider an example where a list L contains five integers stored in a random fashion, as
shown:

• Now, if the list L is sorted using selection sort technique then first of all the first element in the

list, i.e., 18 will be selected and compared with all the remaining elements in the list.

• The element which is found to be the lowest amongst the remaining set of elements will be
swapped with the first element.

• Then, the second element will be selected and compared with the remaining elements in the list.

• This process is repeated until all the elements are rearranged in a sorted manner.

Passes of Selection Sort

• A single iteration of the selection sorting technique that brings the smallest element at the
beginning of the list is called a pass.

• As we can see in the below, four passes were required to sort a list of five elements.

• Hence, we can say that selection sort requires n-1 passes to sort an array of n elements.
Efficiency of Selection Sort

• Assume that an array containing n elements is sorted using selection sort technique.

• Now, the number of comparisons made during first pass = n – 1.


• Number of comparisons made during second pass = n –2.

• Number of comparisons made during last pass = 1.

• So, total number of comparisons

• = (n – 1) + (n – 2) + . . . + 1

• = n * (n – 1) / 2

• = O(n2)

• Thus, efficiency of selection sort = O(n2)

Analysis of Selection Sort

• Best case analysis : O(n2)

• Average case analysis : O(n2)

• Worst case analysis : O(n2)

Advantages of Selection Sort

• It is one of the simplest of sorting techniques.

• It is easy to understand and implement.

• It performs well in case of smaller lists.

• It does not require additional memory space to perform sorting.

Limitations of Insertion Sort

• The efficiency of O(n2) is not well suited for large sized lists.

• It does not leverage the presence of any existing sort pattern in the list.

Contains Duplicate

• Given an integer array nums, print true if any value appears at least twice in the array, and print
false if every element is distinct.

• Input: nums = [1,2,3,1]

• Output: true

• Input: nums = [1,2,3,4]

• Output: false

#include<stdio.h>

#include<math.h>
int main()

int n,f=0;

scanf("%d", &n);

int a[n];

for(int i=0; i<n; i++)

scanf("%d", &a[i]);

int i, j, min, temp;

for (i = 0; i < n - 1; i++)

min = i;

for (j = i + 1; j < n; j++)

If (a[j] < a[min])

min = j;

temp = a[i];

a[i] = a[min];

a[min] = temp;

for( i=0;i<n -1;i++)

if(a[i] == a[i + 1])

f=1;

if(f)

printf("false");

else

printf("true");

}
Missing Number

• Given an array nums containing n distinct numbers in the range [0, n], return the only number in
the range that is missing from the array.

• Input: nums = [3,0,1]

• Output: 2

• Explanation: n = 3 since there are 3 numbers, so all numbers are in the range [0,3]. 2 is the missing

number in the range since it does not appear in nums.

for(int i =0;i<numsSize;i++)

arr[nums[i]] = nums[i];

for(int i =0;i<n;i++)

if(arr[i]!=i)

ans = i;

How Many Numbers Are Smaller Than the Current Number

• Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it.

• That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] <
nums[i].

• Input: nums = [8,1,2,2,3]

• Output: [4,0,1,1,3]

• Explanation:

• For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).

• For nums[1]=1 does not exist any smaller number than it.

• For nums[2]=2 there exist one smaller number than it (1).

• For nums[3]=2 there exist one smaller number than it (1).

• For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).

int i = 0;

for(i = 0 ; i < numsSize ; i ++)

sums[nums[i] + 1]++;
for(i = 1 ; i < 102 ; i++)

sums[i] += sums[i-1];

for(i = 0 ; i < numsSize ; i++)

nums[i] = sums[nums[i]];

You might also like