Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C PROGRAMMING(BTPS101-18) SORTING WITH DIFFERENT METHODS, Study notes of C programming

THIS DOC IS FOR SORTING WITH THEIR DIFFERENT METHODS. BUBBLE SORT WITH PROGRAM INSERTION SORT WITH PROGRAM SELECTION SORT WITH PROGRAM EACH AND EVERY POINT EXPLAINED IN DETAIL. CHECK IT OUT FOR A VERY REASONABLE PRICE .

Typology: Study notes

2024/2025

Available from 07/05/2025

KANOYA2786
KANOYA2786 🇮🇳

11 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
B TECH COMPUTER SCIENCE
PROGRAMMING FOR PROBLEM SOLVING(C)
SUBJECT CODE-BTPH101-23
SORTING AND THEIR DIFFERENT METHODS
Sorting:- Sorting means arranging the elements in an array so that they are placed in
ascending or descending order. For example, an unsorted array a[5] = {3, 10, 11, 2, 1} becomes
sorted in ascending order as a[5] = {1, 2, 3, 10, 11}.
METHODS:-
Bubble
Selection
Insertion
Sorting Techniques
Bubble Sort
Bubble sort is a very simple method that sorts array elements by repeatedly moving the
largest element to the highest index position of the array segment. In bubble sort,
consecutive adjacent elements in the array are compared with each other. If the element at
the lower index is greater than the element at the higher index, the two elements are
swapped.
PROGRAM:-
#include <stdio.h>
int main() {
int a[5] = {5, 2, 15, 22, 1};
int i, j, temp;
for (i = 4; i > 0; i--) {
for (j = 0; j <= i - 1; j++) {
if (a[j] > a[j+1]) {
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
pf3
pf4
pf5

Partial preview of the text

Download C PROGRAMMING(BTPS101-18) SORTING WITH DIFFERENT METHODS and more Study notes C programming in PDF only on Docsity!

B TECH COMPUTER SCIENCE

PROGRAMMING FOR PROBLEM SOLVING(C)

SUBJECT CODE-BTPH101-

SORTING AND THEIR DIFFERENT METHODS

Sorting:- Sorting means arranging the elements in an array so that they are placed in

ascending or descending order. For example, an unsorted array a[5] = {3, 10, 11, 2, 1} becomes sorted in ascending order as a[5] = {1, 2, 3, 10, 11}.

METHODS:-

Bubble

Selection

Insertion

Sorting Techniques

Bubble Sort

Bubble sort is a very simple method that sorts array elements by repeatedly moving the largest element to the highest index position of the array segment. In bubble sort, consecutive adjacent elements in the array are compared with each other. If the element at the lower index is greater than the element at the higher index, the two elements are swapped.

PROGRAM:-

#include <stdio.h> int main() { int a[5] = {5, 2, 15, 22, 1}; int i, j, temp; for (i = 4; i > 0; i--) { for (j = 0; j <= i - 1; j++) { if (a[j] > a[j+1]) { temp = a[j]; a[j] = a[j+1]; a[j+1] = temp;

printf("The sorted array is = "); for (i = 0; i < 5; i++) { printf("%d\n", a[i]); } return 0; }

OUTPUT: 1 2 5 15 22

Bubble Sort Algorithm Steps:

  1. Start
  2. Bubble sort (a, N) where N is the size of the array
  3. Repeat step 2 for i = 4 to i >= 0
  4. Repeat for j = 0 to i-
  5. If (a[j] > a[j+1]), swap a[j] and a[j+1]
  6. Exit

Selection Sort

The following table is an example: 10, 4, 6, 3, 2 0, 1, 2, 3, 4 (index position) For i = 0 (i <= 4 or i < 5)

min = a[0] = 10

To travel elements: j = i + 1, j <= 4

If a[j] < a[min]

For j = 1: a[1] < a[0] => 4 < 10 (True). min = a[1]

■ Assume min = i

■ For (j = i + 1; j <= 4; j++)

■ If a[j] < a[min]

■ min = j

  1. Swap: ○ temp = a[i]

○ a[i] = a[min]

○ a[min] = temp

  1. For Output
  2. End

Insertion Sort :-

Example: a[5] = {10, 4, 6, 3, 2}

#include <stdio.h> int main() { int a[5] = {10, 4, 6, 3, 2}; int i, j, temp;

for (i = 1; i < 5; i++) { // store element in variables of temp temp = a[i]; for (j = i; j > 0 && temp < a[j-1]; j--) { // vacant space a[j] = a[j-1]; } a[j] = temp; }

for (i = 0; i < 5; i++) { printf("%d\n", a[i]); } return 0; }

Explanation/Definition: Insertion sort is a simple sorting algorithm that builds a sorted array one element at a time.