0% found this document useful (0 votes)
9 views

Bubble Sort

Bubble sort is an algorithm that compares and swaps adjacent elements until sorted. It works by moving larger elements to the end of the array in iterations, like air bubbles rising in water. Quicksort is a divide and conquer algorithm that selects a pivot element and partitions the array into subarrays of smaller and larger elements, recursively sorting them until single elements remain and can be combined into a sorted array.

Uploaded by

kschauhan8007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Bubble Sort

Bubble sort is an algorithm that compares and swaps adjacent elements until sorted. It works by moving larger elements to the end of the array in iterations, like air bubbles rising in water. Quicksort is a divide and conquer algorithm that selects a pivot element and partitions the array into subarrays of smaller and larger elements, recursively sorting them until single elements remain and can be combined into a sorted array.

Uploaded by

kschauhan8007
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Bubble Sort

Bubble sort is a sorting algorithm that compares two adjacent elements and swaps them until
they are in the intended order.

Just like the movement of air bubbles in the water that rise up to the surface, each element of
the array move to the end in each iteration. Therefore, it is called a bubble sort.

Working of Bubble Sort


Suppose we are trying to sort the elements in ascending order.

1. First Iteration (Compare and Swap)

 Starting from the first index, compare the first and the second elements.
 If the first element is greater than the second element, they are swapped.
 Now, compare the second and the third elements. Swap them if they are not in order.
 The above process goes on until the last element.
2. Remaining Iteration
The same process goes on for the remaining iterations.

After each iteration, the largest element among the unsorted elements is placed at the end .

The array is sorted when all the unsorted elements are placed at their correct positions.
Quicksort Algorithm
Quicksort is a sorting algorithm based on the divide and conquer
approach where
1. An array is divided into subarrays by selecting a pivot element (element
selected from the array).

While dividing the array, the pivot element should be positioned in such a way
that elements less than pivot are kept on the left side and elements greater
than pivot are on the right side of the pivot.
2. The left and right subarrays are also divided using the same approach. This
process continues until each subarray contains a single element.

3. At this point, elements are already sorted. Finally, elements are combined to
form a sorted array.

Working of Quicksort Algorithm


1. Select the Pivot Element
There are different variations of quicksort where the pivot element is selected
from different positions. Here, we will be selecting the rightmost element of the
array as the pivot element.
Select a pivot element

You might also like