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

#Include Stdio.h

The document presents a C program that implements bubble sort using both an array and a linked list. It includes functions for creating a linked list, sorting it, and displaying the sorted results, as well as sorting an array of integers. The program provides a menu for users to choose between sorting an array or a linked list.

Uploaded by

arjunssagar3
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)
3 views

#Include Stdio.h

The document presents a C program that implements bubble sort using both an array and a linked list. It includes functions for creating a linked list, sorting it, and displaying the sorted results, as well as sorting an array of integers. The program provides a menu for users to choose between sorting an array or a linked list.

Uploaded by

arjunssagar3
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/ 5

BUBBLE SORT USING ARRAY AND LINKED LIST

#include <stdio.h>

#include <stdlib.h>

typedef struct node {

int data;

struct node *next;

} NODE;

NODE *head = NULL, *tail, *ptr;

void create() {

int data = 1;

printf("Enter the data (0 to exit): ");

scanf("%d", &data);

while (data != 0) {

NODE *newnode = (NODE*)malloc(sizeof(NODE));

newnode->data = data;

newnode->next = NULL;

if (head == NULL) {

head = newnode;

} else {

ptr = head;

while (ptr->next != NULL) {

ptr = ptr->next;

ptr->next = newnode;

printf("Enter the data (0 to exit): ");

scanf("%d", &data);
}

void bubble_sort() {

NODE *i, *j;

int temp;

for (i = head; i->next != NULL; i = i->next) {

for (j = i->next; j != NULL; j = j->next) {

if (i->data > j->data) {

temp = i->data;

i->data = j->data;

j->data = temp;

void display() {

NODE *temp = head;

while (temp != NULL) {

printf("%d ", temp->data);

temp = temp->next;

printf("\n");

void bubblesortarray() {

int a[20], n, i, j;

printf("Enter the size of the array: ");

scanf("%d", &n);

printf("Enter the elements of the array:\n");


for (i = 0; i < n; i++) {

scanf("%d", &a[i]);

for (i = 0; i < n - 1; i++) {

for (j = 0; j < n - i - 1; j++) {

if (a[j] > a[j + 1]) {

int temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

printf("Sorted array:\n");

for (i = 0; i < n; i++) {

printf("%d ", a[i]);

printf("\n");

int main() {

int ch;

do {

printf("MENU\n1. Sorting by Array\n2. Sorting by Linked List\n");

printf("Enter your choice: ");

scanf("%d", &ch);

switch (ch) {

case 1:

bubblesortarray();

break;

case 2:
create();

display();

bubble_sort();

display();

break;

default:

printf("Enter a valid choice.\n");

} while (ch != 0);

return 0;

MENU

1. Sorting by Array

2. Sorting by Linked List

Enter your choice: 1

Enter the size of the array: 5

Enter the elements of the array:

Sorted array:

1 2 3 4 5

MENU

1. Sorting by Array

2. Sorting by Linked List

Enter your choice: 2

Enter the data (0 to exit): 3

Enter the data (0 to exit): 1

Enter the data (0 to exit): 4

Enter the data (0 to exit): 5


Enter the data (0 to exit): 2

Enter the data (0 to exit): 0

Unsorted linked list:

3 1 4 5 2

Sorted linked list:

1 2 3 4 5

Excuted By Arjun Sagar

You might also like