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

BubbleSort_Output

The document provides 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. The program features a menu for the user 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)
5 views

BubbleSort_Output

The document provides 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. The program features a menu for the user 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;

Expected Output:

1. For Array:

Enter the size of the array: 5

Enter the elements of the array:

34 12 5 67 89

Sorted array:

5 12 34 67 89

2. For Linked List:

Enter the data (0 to exit): 45

Enter the data (0 to exit): 23

Enter the data (0 to exit): 78

Enter the data (0 to exit): 12

Enter the data (0 to exit): 0

Before Sorting: 45 23 78 12

After Sorting: 12 23 45 78

You might also like