0% found this document useful (0 votes)
13 views4 pages

Practical No 6 - 7 Bubble Sort

Uploaded by

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

Practical No 6 - 7 Bubble Sort

Uploaded by

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

// Practical No 6 WAP to perform bubble sort on numbers

#include <stdio.h>

int main()

int array[100], n, i, j, temp;

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

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

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

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

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

if (array[j] > array[j+1]) /* For decreasing order use '<' instead of '>' */

temp = array[j];

array[j] = array[j+1];

array[j+1] = temp;

printf("Sorted list in ascending order:\n");

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

printf("%d\n", array[i]);
return 0;

OUTPUT:-

Enter number of elements

Enter 4 integers

8692

Sorted list in ascending order:

9
// Practical NO 7 WAP to perform Bubble sort on String data
#include <stdio.h>

#include <string.h>

int main() {

char name[25][50], temp[25]; // Declares an array of strings and a temporary string

int n, i, j; // Declare variables for number of strings and iteration

printf("\n\nSorts the strings of an array using bubble sort :\n"); // Display information about the
task

printf("-----------------------------------------------------\n");

printf("Input number of strings: ");

scanf("%d", &n); // Read the number of strings from the user

printf("Input string %d :\n", n);

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

fgets(name[i], sizeof(name[i]), stdin); // Read strings from the user

/* Logic for Bubble Sort */

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

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

if (strcmp(name[j], name[j + 1]) > 0) { // Compare adjacent strings

strcpy(temp, name[j]); // Swap strings using temporary variable

strcpy(name[j], name[j + 1]);

strcpy(name[j + 1], temp);

printf("The strings appear after sorting:\n"); // Display the sorted strings


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

printf("%s\n", name[i],stdin);

return 0; // Return 0 to indicate successful execution of the program

OUTPUT:-

Sorts the strings of an array using bubble sort :

-----------------------------------------------------

Input number of strings: 4

Input string 4 :

Digital Techniques

Data base Management System

Data Structures

Data Communication and networking

-----------------------------------------------------

The strings appear after sorting:

Data Communication and networking

Data Structures

Data base Management System

Digital Techniques

-----------------------------------------------------

You might also like