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

Sorting Techs (1)

The document provides an overview of five sorting algorithms: Selection Sort, Bubble Sort, Insertion Sort, Quick Sort, and Merge Sort, detailing their mechanisms, time complexities, and space complexities. Each algorithm is illustrated with a code example and an original and sorted array. The time complexities range from O(n^2) for simpler algorithms to O(n log n) for more efficient ones like Quick Sort and Merge Sort.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Sorting Techs (1)

The document provides an overview of five sorting algorithms: Selection Sort, Bubble Sort, Insertion Sort, Quick Sort, and Merge Sort, detailing their mechanisms, time complexities, and space complexities. Each algorithm is illustrated with a code example and an original and sorted array. The time complexities range from O(n^2) for simpler algorithms to O(n log n) for more efficient ones like Quick Sort and Merge Sort.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

1.

Selection Sort

- Works by iterating through the array, selecting the


smallest (or largest) element, and swapping it with the
first (or last) element. i.e finding minimum element and
replace with first index value.
- Time complexity: O(n^2)
- Space complexity: O(1)
- Example:

Original array: [5, 2, 4, 6, 1, 3]


Sorted array: [1, 2, 3, 4, 5, 6]
#include <stdio.h>
int main()
{
int arr[] = {6,1,3,2,5};
int i, j, min,n = 5;;
if (min != i)
for (i = 0; i < n - 1; i++) {
{ int temp = arr[i];
min = i; arr[i] = arr[min];
for (j = i + 1; j < n; j++) arr[min] = temp;
{ }
if (arr[j] < arr[min]) }
{
min = j; printf("Sorted array: ");
} for (i = 0; i < n; i++)
} {
printf("%d ", arr[i]);
}
return 0;
}
2. Bubble Sort
- Works by repeatedly iterating through the
array, comparing adjacent elements and
swapping them if they're in the wrong order.

- Time complexity: O(n^2)


- Space complexity: O(1)
- Example:
Original array: [5, 2, 4, 6, 1, 3]
Sorted array: [1, 2, 3, 4, 5, 6]
#include <stdio.h>
int main()
{
int i,j,n,temp;

int arr[] = {64, 34, 25, 12, 22, 11, 90};

n = 7;
for (i = 0; i < n - 1; i++)
{ printf("Sorted array: ");
for (j = 0; j < n - i - 1; j++)
{
if (arr[j] > arr[j + 1]) for ( i = 0; i < n; i++)
{ {
temp = arr[j]; printf("%d ", arr[i]);
arr[j] = arr[j + 1]; }
arr[j + 1] = temp;
}
return 0;
} }
}
3.Insertion Sort
-Works by iterating through the array one element at
a time, inserting each element into its proper
position within the previously sorted portion of the
array.
- Time complexity: O(n^2)
-Space complexity: O(1)
- Example:
Original array: [5, 2, 4, 6, 1, 3]
Sorted array: [1, 2, 3, 4, 5, 6]
/*3. Insertion Sort
Insertion sort is a simple sorting algorithm that builds the final sorted array (or
list) one item at a time.
*/
#include <stdio.h>
int main()
{
int arr[] = {4,3,1,2,5};
int n = sizeof(arr) / sizeof(arr[0]); printf("Sorted array: ");
int i, key, j;
for (i = 1; i < n; i++) for ( i = 0; i < n; i++)
{ {
key = arr[i]; printf("%d ", arr[i]);
j = i - 1; }
while (j >= 0 && arr[j] > key) return 0;
{ }
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
4. Quick Sort:
QuickSort is a sorting algorithm based on the Divide and
Conquer algorithm that picks an element as a pivot and
partitions the given array around the picked pivot by placing
the pivot in its correct position in the sorted array.

- Works by selecting a pivot element, partitioning the


array around it, and recursively sorting the sub arrays.

- Time complexity: O(n log n) on average,


O(n^2) in the worst case
- Space complexity: O(log n)

- Example:
Original array: [5, 2, 4, 6, 1, 3]
Sorted array: [1, 2, 3, 4, 5, 6]
//Quick Sort
#include <stdio.h>
// Function to swap two elements
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
} // Function to partition the array
int partition(int arr[], int low, int high)
{
int pivot = arr[high]; // Choose the last element as the pivot
int i = low - 1;

for (int j = low; j < high; j++)


{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
// Function to implement Quick Sort
void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
} int main()
{
// Function to print the array int arr[] = {64, 34, 25, 12, 22, 11, 90};
void printArray(int arr[], int size) int n = sizeof(arr) / sizeof(arr[0]);
{ printf("Original array: ");
for (int i = 0; i < size; i++) printArray(arr, n);
{ quickSort(arr, 0, n - 1);
printf("%d ", arr[i]); printf("Sorted array: ");
} printArray(arr, n);
printf("\n"); return 0;
} }
. Merge Sort

-Works by dividing the array into smaller chunks,


sorting each chunk recursively, and merging the sorted
chunks back together.

- Time complexity: O(n log n)


- Space complexity: O(n)
- Example:

Original array: [5, 2, 4, 6, 1, 3]


Sorted array: [1, 2, 3, 4, 5, 6]
#include <stdio.h>

// Function to merge two sorted subarrays


void merge(int arr[], int left, int mid, int right)
{
int n1 = mid - left + 1;
int n2 = right - mid;

int leftArr[n1], rightArr[n2];

// Copy data to temporary arrays


for (int i = 0; i < n1; i++)
{
leftArr[i] = arr[left + i];
}
for (int j = 0; j < n2; j++)
{
rightArr[j] = arr[mid + 1 + j];
}

int i = 0, j = 0, k = left;
// Merge the temporary arrays
while (i < n1 && j < n2)
{
if (leftArr[i] <= rightArr[j])
{
arr[k] = leftArr[i]; // Copy remaining elements of rightArr[], if there are any
i++; while (j < n2)
} {
else arr[k] = rightArr[j];
{ j++;
arr[k] = rightArr[j]; k++;
j++; }
} }
k++;
}
// Copy remaining elements of leftArr[], if there are any
while (i < n1)
{
arr[k] = leftArr[i];
i++;
k++;
}
// Function to implement Merge Sort
void mergeSort(int arr[], int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;

// Recursively sort the subarrays


mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);

// Merge the sorted subarrays


merge(arr, left, mid, right);
}
}
}
// Function to print the array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
}
int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Original array: ");
printArray(arr, n);
mergeSort(arr, 0, n - 1);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}

You might also like