PPS Source.á ?2
PPS Source.á ?2
Digember Kumar
Asst. Professor
Department of Computer Science and Engineering
N.C.E. Chandi
What is Sorting?
Example: 2,3,1,5,8,6
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);
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.
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).