0% found this document useful (0 votes)
56 views16 pages

Insertion Sort:-: Output

The document provides code snippets for implementing various sorting algorithms and data structures including insertion sort, selection sort, merge sort, bubble sort, heap sort, shell sort, quick sort, doubly linked list, singly linked list, and circular linked list. Each algorithm or data structure is explained through a C code implementation, sample input/output, and brief description.

Uploaded by

Nitesh Nayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views16 pages

Insertion Sort:-: Output

The document provides code snippets for implementing various sorting algorithms and data structures including insertion sort, selection sort, merge sort, bubble sort, heap sort, shell sort, quick sort, doubly linked list, singly linked list, and circular linked list. Each algorithm or data structure is explained through a C code implementation, sample input/output, and brief description.

Uploaded by

Nitesh Nayak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

Insertion Sort :-
#include <stdio.h>
void insertion_sort(int arr[], int n) {
int i, j, key;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--; }
arr[j + 1] = key; } }
int main() {
int arr[] = {5, 2, 4, 6, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
insertion_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0; }

OUTPUT :-
Sorted array: 1 2 3 4 5 6
2. Selection SORT :-
#include <stdio.h>
void selection_sort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j; } }
int temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp; } }

int main() {
int arr[] = {5, 2, 4, 6, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
selection_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT :-
Sorted array: 1 2 3 4 5 6
3.Merge SORT :-
#include <stdio.h>
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
int i, j, k;
for (i = 0; i < n1; i++) {
L[i] = arr[l + i]; }
for (j = 0; j < n2; j++) {
R[j] = arr[m + 1 + j]; }
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++; }
else {
arr[k] = R[j];
j++; }
k++; }
while (i < n1) {
arr[k] = L[i];
i++;
k++; }
while (j < n2) {
arr[k] = R[j];
j++;
k++; } }
void merge_sort(int arr[], int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
merge_sort(arr, l, m);
merge_sort(arr, m + 1, r);
merge(arr, l, m, r) } }
int main() {
int arr[] = {5, 2, 4, 6, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
merge_sort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]); }
return 0; }
OUTPUT :-
Sorted array: 1 2 3 4 5 6
4. Bubble Sort :-
#include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp; } } } }
int main() {
int arr[] = {5, 2, 4, 6, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]); }
return 0; }
OUTPUT :-
Sorted array: 1 2 3 4 5 6
5. Heap Sort :-
#include <stdio.h>
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest]) {
largest = l; }
if (r < n && arr[r] > arr[largest]) {
largest = r; }
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest); } }
void heap_sort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i); }
for (int i = n - 1; i >= 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0); } }
int main() {
int arr[] = {5, 2, 4, 6, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
heap_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT :-
Sorted array: 1 2 3 4 5 6
6. Shell Sort :-
#include <stdio.h>
void shell_sort(int arr[], int n) {
int i, j, gap, temp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
temp = arr[i];
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap]; }
arr[j] = temp; } } }
int main() {
int arr[] = {5, 2, 4, 6, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
shell_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0; }
OUTPUT :-
Sorted array: 1 2 3 4 5 6
7. Quick Sort :-
#include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quick_sort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quick_sort(arr, low, pi - 1);
quick_sort(arr, pi + 1, high);
}
}
int main()
{
int arr[] = {5, 2, 4, 6, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
quick_sort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
return 0;
}

OUTPUT :-
Sorted array: 1 2 3 4 5 6
8. Doubly Linked List :-
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node * preptr;
struct node * nextptr;
}*stnode, *ennode;
void DlListcreation(int n);
void displayDlListRev();
int main() {
int n;
stnode = NULL;
ennode = NULL;
printf("\n\n Doubly Linked List : Create and display a doubly linked list in reverse
order :\n");
printf("------------------------------------------------------------------------------------\n");

printf(" Input the number of nodes : ");


scanf("%d", &n);
DlListcreation(n);
displayDlListRev();
return 0; }
void DlListcreation(int n) {
int i, num;
struct node *fnNode;
if(n >= 1) {
stnode = (struct node *)malloc(sizeof(struct node));
if(stnode != NULL) {
printf(" Input data for node 1 : ");
scanf("%d", &num);
stnode->num = num;
stnode->preptr = NULL;
stnode->nextptr = NULL;
ennode = stnode;
for(i=2; i<=n; i++) {
fnNode = (struct node *)malloc(sizeof(struct node));
if(fnNode != NULL)
{
printf(" Input data for node %d : ", i);
scanf("%d", &num);
fnNode->num = num;
fnNode->preptr = ennode;
fnNode->nextptr = NULL;

ennode->nextptr = fnNode;
ennode = fnNode;
}
else
{
printf(" Memory can not be allocated.");
break; } } }
else {
printf(" Memory can not be allocated."); } } }
void displayDlListRev()
{
struct node * tmp;
int n = 0;
if(ennode == NULL)
{
printf(" No data found in the List yet.");
}
else
{
tmp = ennode;
printf("\n Data in Reverse order are :-\n");
while(tmp != NULL)
{
printf(" Data in node %d : %d\n", n+1, tmp->num);
n++;
tmp = tmp->preptr;
}
}
}

Output :-
Doubly Linked List : Create and display a doubly linked list in reverse order :
------------------------------------------------------------------------------------
Input the number of nodes : 3
Input data for node 1 : 2
Input data for node 2 : 5
Input data for node 3 : 8
Data in Reverse order are :-
Data in node 1 : 8
Data in node 2 : 5
Data in node 3 : 2
9. Singly Linked List :-
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* next; };
void insert_node(struct node** head_ref, int new_data) {
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node; }
void print_list(struct node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next; }
printf("\n"); }
int main() {
struct node* head = NULL;
insert_node(&head, 3);
insert_node(&head, 6);
insert_node(&head, 9);
print_list(head);
return 0; }
Output :-
Forward List: 9 6 3
Backward List: 3 6 9
10. Circular Linked list :-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next; };
struct node *head = NULL;
struct node *tail = NULL;
void add(int data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
if(head == NULL) {
head = newNode;
tail = newNode;
newNode->next = head; }
else {
tail->next = newNode;
tail = newNode;
tail->next = head; } }
void display() {
struct node *current = head;
if(head == NULL)
{
printf("List is empty");
}
Else
{
printf("Nodes of the circular linked list: \n");
do{
printf("%d ", current->data);
current = current->next;
}
while(current != head);
printf("\n");
}
}
int main()
{
add(1);
add(2);
add(3);
add(4);
display();
return 0;
}
OUTPUT :-
Nodes of the circular linked list:
1234

You might also like