Portfolio Chapter 4
Portfolio Chapter 4
Lets consider the following array as an example: arr[] = {64, 25, 12, 22, 11}
1. For the first position in the sorted array, the whole array is traversed from
index 0 to 4 sequentially. The first position where 64 is stored presently, after
traversing whole array it is clear that 11 is the lowest value.
Thus, replace 64 with 11. After one iteration 11, which happens to be the least
value in the array, tends to appear in the first position of the sorted list.
{11,25,12,22,64}
2. For the second position, where 25 is present, again traverse the rest of the
array in a sequential manner.
After traversing, we found that 12 is the second lowest value in the array and it
should appear at the second place in the array, thus swap these values.
{11,12,25,22,64}
3.Now, for third place, where 25 is present again traverse the rest of the array
and find the third least value present in the array.
While traversing, 22 came out to be the third least value and it should appear at
the third place in the array, thus swap 22 with element present at third position.
{11,12,22,25,64}
4. Similarly, for fourth position traverse the rest of the array and find the
fourth least element in the array
As 25 is the 4th lowest value hence, it will place at the fourth position
{11,12,22,25,64}
5. At last the largest value present in the array automatically get placed at the
last position in the array
The resulted array is the sorted array
sorted => {11,12,22,25,64}
total passes: n
Time Complexity: The time complexity of Selection Sort is O(N^2) as there are two
nested loops:
/
***********************************************************************************
********************************************************************/
bubble sort-
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the
adjacent elements if they are in the wrong order. This algorithm is not suitable
for large data sets as its average and worst-case time complexity is quite high.
total passes: n-1
/
***********************************************************************************
********************************************************************/
insertion sort-
you pick an element from the unsorted group and put it in the right place in the
sorted group.
1. We have to start with second element of the array as first element in the array
is assumed to be sorted.
2. Compare second element with the first element and check if the second element is
smaller then swap them.
3. Move to the third element and compare it with the second element, then the first
element and swap as necessary to put it in the correct position among the first
three elements.
4. Continue this process, comparing each element with the ones before it and
swapping as needed to place it in the correct position among the sorted elements.
5. Repeat until the entire array is sorted.
total passes n
return 0;
}
/
***********************************************************************************
*************************************************************/
quick sort-
QuickSort is one of the best sorting algorithms developed by Tony Hoare in 1960 to
sort the array. It follows the divide-and-conquer rule like Merge Sort but unlike
Merge Sort, this algorithm does not use any extra space for sorting (though it uses
an auxiliary stack space).
The basic idea behind QuickSort is to select a “pivot” element from the array and
partition the other elements into two sub-arrays according to whether they are less
than or greater than the pivot.
Pick an element: Randomly choose any element from the pile. Call this element
“pivot.”
Divide the Pile: Separate the remaining elements into two piles – one pile having
elements that are smaller than the pivot and another with elements larger than the
pivot.
Sort the Piles: Now, repeat the same process of dividing each of the two piles
until each pile only has one element left.
Combine Sorted Piles: When all the sub piles are sorted, put them together in order
such that the smallest is on the left and the largest on the right.
8 7 6 1 0 9 2
p2 pivot
if array[0] > pivot then place p2 in array[0]
8 7 6 1 0 9 2
p2 p3 pivot
if p3 > pivot then p3++
if p3 < pivot then value(p2) <-> value(p3) , p2++, p3++
8 7 6 1 0 9 2
p2 p3 pivot
8 7 6 1 0 9 2
p2 p3 pivot
1 7 6 8 0 9 2
p2 p3 pivot
1 0 6 8 7 9 2
p2 p3 pivot
once p3 reaches just before pivot i.e p3 = pivot-1, value(p2)<->value(pivot)
1 0 2 8 7 9 6
divide from pivot
1 0 | 2 | 8 7 9 6
/
***********************************************************************************
*************************************************************/
merge sort-
Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides the input
array into two halves, calls itself for the two halves, and then it merges the two
sorted halves.
At first, check if the left index of array is less than the right index, if yes
then calculate its mid point
{38,27,43,3,9,82,10}
Now, as we already know that merge sort first divides the whole array iteratively
into equal halves, unless the atomic values are achieved.
Here, we see that an array of 7 items is divided into two arrays of size 4 and 3
respectively.
{38,27,43,3} {9,82,10}
Now, again find that is left index is less than the right index for both arrays, if
found yes, then again calculate mid points for both the arrays.
{38,27} {43,3} {9,82} {10}
Now, further divide these two arrays into further halves, until the atomic units of
the array is reached and further division is not possible.
{38} {27} {43} {3} {9} {82} {10}
After dividing the array into smallest units, start merging the elements again
based on comparison of size of elements
Firstly, compare the element for each list and then combine them into another list
in a sorted manner.
{27,38} {3,43} {9,82} {10}
After the final merging, the list looks like this:
{3,27,38,43} {9,10,82}
{3,9,10,27,38,43,82}
merge(arr, l, m, r);
}
}
// UTILITY FUNCTIONS
// Function to print an array
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", A[i]);
printf("\n");
}
// Driver code
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
/
***********************************************************************************
*******************************************************************/
Linear Search
Binary Search
Linear search
Assume that item is in an array in random order and we have to find an item. Then
the only way to search for a target item is, to begin with, the first position and
compare it to the target. If the item is at the same, we will return the position
of the current item. Otherwise, we will move to the next position. If we arrive at
the last position of an array and still can not find the target, we return -1. This
is called the Linear search or Sequential search.
// Linear Search in C
#include <stdio.h>
int main() {
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 10;
if (result == -1) {
printf("Element is not present in array\n");
} else {
printf("Element is present at index %d\n", result);
}
return 0;
}
BINARY SEARCH
In a binary search, however, cut down your search to half as soon as you find the
middle of a sorted list. The middle element is looked at to check if it is greater
than or less than the value to be searched. Accordingly, a search is done to either
half of the given list.
example-
0 1 2 3 4 5 6 7 8
10 12 24 29 39 40 51 56 69
to search k=29, beg=0, end=8
mid = (beg+end)/2 = 0+8/2 = 4
array[mid] = 39
array[mid]>k (i.e 29)
therefore, end = mid-1 = 3
beg remains same = 0
mid = (beg+end)/2 = 0+3/2 = 1(approx.)
array[mid] = 12
array[mid]<k (i.e 29)
therefore, beg = mid+1 = 2
end remains same = 3
mid = (beg+end)/2 = 5/2 = 2(approx.)
array[mid] = 24
array[mid]<k (i.e 29)
therefore, beg = mid+1 = 3
end remains same = 3
mid = (beg+end)/2 = 6/2 = 3
array[mid] = k
therefore, ans = mid = 3 (third element)
if (array[mid] == k) {
return mid; // Element found at middle index
} else if (array[mid] > k) {
return binary_search(array, k, beg, mid - 1); // Search left sub-array
} else {
return binary_search(array, k, mid + 1, end); // Search right sub-array
}
}
int main() {
int array[] = {2, 5, 8, 12, 16};
int n = sizeof(array) / sizeof(array[0]);
int k = 12;
if (ans == -1) {
printf("Element not found\n");
} else {
printf("Element found at index %d\n", ans);
}
return 0;
}
/
***********************************************************************************
****************************************************************/