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

Sorting

The document explains two sorting algorithms: Insertion Sort and Bubble Sort. Insertion Sort organizes elements by iterating through an unsorted list and placing each element in its correct position within a sorted section, while Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. Both algorithms are demonstrated with C code examples for practical implementation.

Uploaded by

pranita.tathe15
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Sorting

The document explains two sorting algorithms: Insertion Sort and Bubble Sort. Insertion Sort organizes elements by iterating through an unsorted list and placing each element in its correct position within a sorted section, while Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order. Both algorithms are demonstrated with C code examples for practical implementation.

Uploaded by

pranita.tathe15
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Sorting

• Insertion sort is an algorithm used to sort a collection of elements in


ascending or descending order. The basic idea behind the algorithm is to
divide the list into two parts: a sorted part and an unsorted part.
• Initially, the sorted part contains only the first element of the list, while the
rest of the list is in the unsorted part. The algorithm then iterates through
each element in the unsorted part, picking one at a time, and inserts it into
its correct position in the sorted part.
• To do this, the algorithm compares the current element with each element in
the sorted part, starting from the rightmost element. It continues to move to
the left until it finds an element that is smaller (if sorting in ascending order)
or larger (if sorting in descending order) than the current element.
• Insertion Sort Algorithm
• To sort an array of size N in ascending order:
• Iterate from arr[1] to arr[N] over the array.
• Compare the current element (key) to its predecessor.
• If the key element is smaller than its predecessor, compare it to the
elements before. Move the greater elements one position up to make
space for the swapped element.
• // C program for insertion sort
• #include <math.h>
• #include <stdio.h>
• /* Function to sort an array
• using insertion sort*/
• void insertionSort(int arr[], int n)
• {
• int i, key, j;
• for (i = 1; i < n; i++)
• {
• key = arr[i];
• j = i - 1;
• /* Move elements of arr[0..i-1],
• that are greater than key,
• to one position ahead of
• their current position */
• while (j >= 0 && arr[j] > key)
• {
• arr[j + 1] = arr[j];
• j = j - 1;
• }
• arr[j + 1] = key;
• }
• }
• void printArray(int arr[], int n)
• {
• int i;
• for (i = 0; i < n; i++)
• printf("%d ", arr[i]);
• printf("\n");
• }

• // Driver code
• int main()
• {
• int arr[] = {12, 11, 13, 5, 6};
• int n = sizeof(arr) / sizeof(arr[0]);

• insertionSort(arr, n);
• printArray(arr, n);

• return 0;
• }
• What is Bubble Sort?
• Bubble sort is a simple sorting algorithm that works by comparing the
adjacent elements in the list and swapping them if the elements are
not in the specified order. It is an in-place and stable sorting algorithm
that can sort items in data structures such as arrays and linked lists.
• Bubble Sort Algorithm in C
• The algorithm to sort data of the list in increasing order using bubble sort in
C is:
• Run two loops nested in one another.

• The outer loop will run from i = 0 to i < n – 1, where n is the number of
elements in the list.

• The inner loop will run from j = 0 to j < n – i – 1. It is because, after each
iteration of the outer loop, one element at the end (or at the start if the
order is decreasing order) will be in its right place so we can leave it as it is.
• # #include <stdio.h>

• // Swap function
• void swap(int* arr, int i, int j)
• {
• int temp = arr[i];
• arr[i] = arr[j];
• arr[j] = temp;
• }

• // A function to implement bubble sort
• void bubbleSort(int arr[], int n)
• {
• int i, j;
• for (i = 0; i < n - 1; i++)

• // Last i elements are already
• // in place
• for (j = 0; j < n - i - 1; j++)
• if (arr[j] > arr[j + 1])
• swap(arr, j, j + 1);
• }
• void printArray(int arr[], int size)
• {
• int i;
• for (i = 0; i < size; i++)
• printf("%d ", arr[i]);
• printf("\n");
• }

• // Driver code
• int main()
• {
• int arr[] = { 5, 1, 4, 2, 8 };
• int N = sizeof(arr) / sizeof(arr[0]);
• bubbleSort(arr, N);
• printf("Sorted array: ");
• printArray(arr, N);
• return 0;
• }

You might also like