Write programs for Bubble Sort, Quick sort

Answer Posted / rakesh

/* Write C programs that implement the following sorting
methods to sort
a given list of integers in ascending order: i) Bubble
sort */

#include <stdio.h>
#define MAX 10

void swapList(int *m,int *n)
{
int temp;
temp = *m;
*m = *n;
*n = temp;
}

// Function for Bubble Sort
void bub_sort(int list[], int n)
{
int i,j;
for(i=0;i<(n-1);i++)
for(j=0;j<(n-(i+1));j++)
if(list[j] > list[j+1])
swapList(&list[j],&list[j+1]);
}

void readlist(int list[],int n)
{
int j;
printf("\nEnter the elements: \n");
for(j=0;j<n;j++)
scanf("%d",&list[j]);
}

// Showing the contents of the list
void printlist(int list[],int n)
{
int j;
for(j=0;j<n;j++)
printf("%d\t",list[j]);
}

void main()
{
int list[MAX], num;
clrscr();
printf("\n\n\n***** Enter the number of elements
[Maximum 10] *****\n");
scanf("%d",&num);
readlist(list,num);
printf("\n\nElements in the list before sorting are:\n");
printlist(list,num);
bub_sort(list,num);
printf("\n\nElements in the list after sorting are:\n");
printlist(list,num);
getch();
}

Is This Answer Correct ?    15 Yes 11 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the types of collection?

476


Can arraylist hold duplicates?

492


Briefly explain recursive algorithm 50 how do you search for a target key in a linked list?

577


What is an algorithm in coding?

428


Is bubble sort slow?

525






What is an object array?

529


What is inplace sorting?

597


Write an algorithm to show the reverse of link list?

473


Why quicksort is faster?

491


How would you use qsort() function to sort the name stored in an array of pointers to string?

573


How do you find the length of an arraylist?

488


Explain about the different lists available in the collection?

460


Define 2-3 tree?

564


Explain what is the bucket size, when the overlapping and collision occur at same time?

583


How many links are there in a binary tree of N nodes?

603