Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new

 Categories >> Software >> Operating Systems >> Data Structures

 Windows Interview Questions (340) Linux Interview Questions (453) Unix Interview Questions (458) Solaris Interview Questions (781) RTOS Interview Questions (43) Bulnex Interview Questions (4) Operating Systems General Concepts Interview Questions (261) Data Structures Interview Questions (67) Operating Systems AllOther Interview Questions (48)
Question
`Write programs for Bubble Sort, Quick sort`
Question Submitted By :: Data-Structures
I also faced this Question!!     Answer Posted By

# 1
//PROGRAM FOR BUBBLE SORT

#include<stdio.h>
#include<conio.h>
#define SIZE 5
void main()
{
int a[SIZE],n,i,j,temp;
clrscr();
printf("enter the elements ");
for(i=0;i<SIZE;i++)
scanf("%d",&a[i]);
printf("the sorted list is :->\n");
for(i=0;i<SIZE;i++)
for(j=i;j<SIZE-i;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

for(i=0;i<SIZE;i++)
printf("%d",a[i]);
getch();
}

 Is This Answer Correct ? 173 Yes 94 No
Nitin Jatpuriya

# 2
/*QUICK SORT*/
#include<stdio.h>
#include<conio.h>

int split(int [],int,int);
void quicksort(int [],int,int);

void main()
{
int arr[20],n,i;
clrscr();
printf("\nQUICk SORT\n");
printf("Enter the no.of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("\nArray before sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
quicksort(arr,0,n);
printf("\nArray after sorting:\n");
for(i=0;i<n;i++)
printf("%d\t",arr[i]);
getch();
}

void quicksort(int a[],int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(a,lower,upper);
quicksort(a,lower,i-1);
quicksort(a,i+1,upper);
}
}

int split(int a[],int lower,int upper)
{
int i,p,q,t;
p=lower+1;
q=upper;
i=a[lower];
while(q>=p)
{
while(a[p]<i)
p++;
while(a[q]>i)
q--;
if(q>p)
{
t=a[p];
a[p]=a[q];
a[q]=t;
}
}
t=a[lower];
a[lower]=a[q];
a[q]=t;
return(q);
}

 Is This Answer Correct ? 114 Yes 45 No
Arnoldindia

# 3
void bubbleSort(int numbers[], int array_size)
{
int i, j, temp;

for (i = (array_size - 1); i >= 0; i--)
{
for (j = 1; j <= i; j++)
{
if (numbers[j-1] > numbers[j])
{
temp = numbers[j-1];
numbers[j-1] = numbers[j];
numbers[j] = temp;
}
}
}
}

 Is This Answer Correct ? 57 Yes 17 No
Srinivsa

# 4
//Program for implementing Bubble Sort

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,p,t;
clrscr();
printf("Enter the array limit");
scanf("%d",&n);
printf("\nEnter %d elemts",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
p=0;
while(p<n-i)
{
if(a[p]>a[p+1])
{
t=a[p];
a[p]=a[p+1];
a[p+1]=t;
}
p++;
}
}
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}

 Is This Answer Correct ? 42 Yes 16 No
Cynthia

# 5
write programs for quick sort,merge sort in c++?
 Is This Answer Correct ? 39 Yes 24 No
Ramnivash

# 6
Program for Bubble sort*/
#include<stdio.h>
#include<conio.h>
#define max 20
void insert(int [],int);
void display(int [],int);
void sort(int [],int);
void main()
{
int a[max],n;
clrscr();
printf("\n \t Enter the size of array < %d--->",max);
scanf("%d",&n);
insert(a,n);
printf("\n \t Elements before sorting");
display(a,n);
sort(a,n);
printf("\n \t Elements after sorting");
display(a,n);
}
void insert(int a[],int n)
{
int i;
printf("\n \t Enter %d elements-->",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void display(int a[],int n)
{
int i;
for(i=0;i<n;i++)
printf("\n %d",a[i]);
}
void sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])as
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
} }
}}

Enter the size of array < 20---->5

Enter 5 elements--->45
3
78
43
21

Elements before sorting
45
3
78
43
21
Elements after sorting
3
21
43
45
78

 Is This Answer Correct ? 23 Yes 12 No
Naveen

# 7
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,j,temp;
printf("\n\nEnter the total number of eleents:");
scanf("%d",&n);
printf("\n\nEnter the array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
/* sorting */
for(i=0;i<n;i++)
{
for(j=i;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
printf("\n\nThe sorted array:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

 Is This Answer Correct ? 36 Yes 28 No
Siya

# 8
/* 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]);
}

{
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);
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
Rakesh

# 9
#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,t;
int a[7]={5,7,8,1,4,2,9}; // You can use any length

for(i=0;i<6;i++)
{
for(j=i+1;j<6;j++)
{
if(a[i]>a[j])
{
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
printf("The Sorted Array is = %d",a[i]);

getch();
}

 Is This Answer Correct ? 4 Yes 2 No
Prashant Gupta

# 10
your all coding result is wrong...........
 Is This Answer Correct ? 15 Yes 14 No
Babbu