0% found this document useful (0 votes)
21 views18 pages

PPS Source.á ?2

Uploaded by

amaniitp09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views18 pages

PPS Source.á ?2

Uploaded by

amaniitp09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Sorting in C

Digember Kumar
Asst. Professor
Department of Computer Science and Engineering
N.C.E. Chandi
What is Sorting?

A Sorting Algorithm is used to rearrange a given array or list elements according


to a comparison operator on the elements.
The comparison operator is used to decide the new order of element in the
respective data structure.
In simple word, sorting means arranging the given elements or data in an ordered
sequence.
The main purpose of sorting is to easily & quickly locate an element in a sorted
list.
What is Sorting? Example

Example: 2,3,1,5,8,6

Sorted in Ascending Order(non decreasing order): 1,2,3,5,6,8

Sorted in Descending Order(non increasing order): 8,6,5,3,2,1


What is Sorting? Types

I. Bubble Sort
II. Insertion Sort
III. Selection Sort
IV. Quick Sort
V. Merge Sort
Bubble Sort:
Bubble Sort is a simple sorting algorithm which repeatedly compares the
adjacent elements of the given array & swaps them if they are in wrong order.
Suppose we have an array X which contains n elements which needs to be sorted
using Bubble Sort.
The sorting works as:
Pass 1:
X[0] & X[1] are compared, and swapped if X[0] > X[1]
X[1] & X[2] are compared, and swapped if X[1] > X[2]
X[2] & X[3] are compared, and swapped if X[2] > X[3] and so on…
At the end of pass 1, the largest element of the list is placed at the highest index of the list.
Bubble Sort: cont…
Pass 2:
X[0] & X[1] are compared, and swapped if X[0] > X[1]
X[1] & X[2] are compared, and swapped if X[1] > X[2]
X[2] & X[3] are compared, and swapped if X[2] > X[3] and so on…
At the end of Pass 2 the second largest element of the list is placed at the second highest index of
the list.
Pass 3:
……………
……………
Pass n-1:
X[0] & X[1] are compared, and swapped if X[0] > X[1]
X[1] & X[2] are compared, and swapped if X[1] > X[2]
X[2] & X[3] are compared, and swapped if X[2] > X[3] and so on…
At the end of this pass. The smallest element of the list is placed at the first index of the list.
Bubble Sort Algorithm:
Bubble sort(list- unsorted){
loop := list.count
for i=1 to loop do:
for j= 0 to loop-i do:
if list[j] > list[j+1]:
swap(list[j] > list[j+1])
end if
end for loop
end for loop
return sorted-list
}
1

int main(){
int list[] = {1,8,2,7,4,5};
int i,j, temp;
int len = sizeof(list)/sizeof(int);

for(i=0; i< len; i++){


for(j=0; j < len-i-1; j++){
if(list[j] > list[j+1]){
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;
}
}
}
return 0;
}
Insertion Sort:
1. Insertion sort is a simple sorting algorithm that works similar to the way you sort
playing cards in your hands.
2. The array is virtually split into a sorted and an unsorted part.
3. Values from the unsorted part are picked and placed at the correct position in the
sorted part.
Insertion Sort:
Insertion Sort Algorithm:
Step 1 - If the element is the first element, assume that it is already sorted. Return 1.

Step2 - Pick the next element, and store it separately in a key.

Step3 - Now, compare the key with all elements in the sorted array.

Step 4 - If the element in the sorted array is smaller than the current element, then move
to the next element. Else, shift greater elements in the array towards the right.

Step 5 - Insert the value.

Step 6 - Repeat until the array is sorted.


Insertion Sort Program:
int main(){
int list[] = {1,8,2,7,4,5,12,0};
int i,j, temp;
int len = sizeof(list)/sizeof(int);

for(i=1; i< len; i++){


temp = list[i];
j = i-1;
while(j >= 0 && temp <= list[j]){
list[j+1] = list[j];
j=j-1;
}
list[j+1] = temp;
}
}
Selection Sort:
The selection sort algorithm sorts an array by repeatedly finding the minimum element
(considering ascending order) from unsorted part and putting it at the beginning.
The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending order)
from the unsorted subarray is picked and moved to the sorted subarray.
Selection Sort:
Selection sort has the following characteristics:
1. Comparison-based sorting algorithm.
2. An unstable sorting algorithm i.e. does not preserve the order of duplicate elements.
3. Time complexity is O(n2).
4. Selection sort is the best algorithm when swapping is a costly operation.
Example of selection sort:
arr[] = 64 25 12 22 11

// Find the minimum element in arr[0...4] and place it at beginning


11 25 12 22 64

// Find the minimum element in arr[1...4] and place it at beginning of arr[1...4]


11 12 25 22 64

// Find the minimum element in arr[2...4] and place it at beginning of arr[2...4]


11 12 22 25 64

// Find the minimum element in arr[3...4] and place it at beginning of arr[3...4]


11 12 22 25 64
Algorithm of selection sort:
Step 1: For i = 1 to n-1
step 2: Set min = arr[i]
step 3: Set min_idx = i
step 4: For j = i+1 to n-1 repeat:
if (min > arr[j])
Set min = arr[j]
Set min_idx = j
[end of if]
[end of loop]
step 5: swap a[i] with arr[min_idx]
[end of loop]
step 6: END
int main()
{ int arr[] = {64, 25, 12, 22, 11};
1

int n = sizeof(arr)/sizeof(arr[0]);
int i, j, min_idx,temp;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++){
if (arr[j] < arr[min_idx]){
min_idx = j;
}
}
temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
return 0;
}
Complexity of an Algorithm:
1. Algorithmic complexity in computer science refers to the measure of the number of elementary
operations required for the execution of an algorithm, based on the size of the problem
instance.
2. It is represented by a function denoted as O(f(n)), where f is a function and n is the size of the
problem instance.
3. Complexity in algorithms refers to the amount of resources (such as time or memory) required
to solve a problem or perform a task.
4. The two main types of complexity in data structures are time complexity and space complexity.
5. Time complexity: How long it takes to execute an operation on a data structure, such as
searching, inserting, deleting, or sorting.
6. Space complexity: How much memory or storage is required to store a data structure.
7. Time Complexity for all three: bubble sort, insertion sort and selection sort is O(n2).

You might also like