 Articles

Forum
 Pages (2)  2   ( Go to first unread post )

# Bubble Sort, Code snippet for a bubble sorting technique

vulPeCula Extremely Active Member      Group: Members
Posts: 190
Member No.: 16362
Joined: 16-December 08 Hello guys this is a code for bubble sort as sorting techniques are used at some places so here's a code and exe file.
[Code is written in C and compiled in Borland C IDE].

I hope its useful.

Regards,
vulPeCula.  Login or Register to download
 Jupiter Extremely Active Member      Group: Moderators Posts: 738 Member No.: 773 Joined: 10-November 04 Thank you for sharing.Fix typos in source:teh -> theTemprary -> Temporary
 vulPeCula Extremely Active Member      Group: Members Posts: 190 Member No.: 16362 Joined: 16-December 08   Elena New Member Group: Members Posts: 3 Member No.: 47659 Joined: 14-November 11 Implementing Bubble Sort in C#using System; class AscendingBubbleSort { public static void Main() { int i,j,t; int []p=new int; int []q=new int; int []c=new int; for(i=0;i<10;i++) { Console.WriteLine("Enter Value"); p[i]=int.Parse(Console.ReadLine()); } for(i=0;i<10;i++) { Console.WriteLine("Enter Value"); q[i]=int.Parse(Console.ReadLine()); } if(j<10) c[j]=p[j]; else c[j]=q[j-10]; // Sorting: Bubble Sort for(i=0;i<20;i++) { for(j=0;j<20;j++) { if(c[i]>c[j]) { t=c[i]; c[i]=c[j]; c[j]=t; } } } // Print the contents of the sorted array for(i=0;i<20;i++) { Console.WriteLine (c[i]); } } }
Cosmin_NTG

New Member Group: Members
Posts: 1
Member No.: 49432
Joined: 7-February 12 That's not the most optimized implementation of Bubble sort because the program performs n^2 steps for sorting that array (those steps are performed even the array is already sorted). An optimized implementation is this:
 CODE #includeusing namespace std;int main(){  int n, v, i, aux, x;    cout<<"n="; cin>>n;    for(i=1; i<=n; i++) {cout<<"v["<>v[i];}    do{    x=0;   for(i=1; i<=n-1; i++)      if(v[i]>v[i+1]        {   aux=v[i]; v[i]=v[i+1]; v[i+1]=aux; x=1; }   while(x);  for(i=1; i<=n; i++) cout<

The program should compile ok (I didn't compile it) on any GNU GCC compiler. It's written in C++.
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members: