473,320 Members | 1,580 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

bubble sort query

1
Hi all

I am trying to wirte a program to sort an arry of size 10 into ascending order. I also need to ensure that after the second pass it only makes eight comparisons , seven compairisions on the third pass and so on as at this stage the highest numbers are in thier place. Can anyone advise how I can change my for loop to do this, I seem to making nine comparisons oneach pass.

Thanks

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define SIZE 10
  3.  
  4. int main()
  5. {
  6.    int a[SIZE] = { 2,6,4,6,20,12,89,68,45,37 };
  7.    int i; /*inner counter*/
  8.    int pass; /*outer counter*/
  9.    int hold; /* temporary location used to swap array elements*/
  10.  
  11.    printf( ''Data items in original order\n'');
  12.  
  13.    /*output original array*/
  14.    for( i=0, i< SIZE; i++) {
  15.       printf( ''%d'', a[i] ) ;
  16.    }
  17.  
  18.    /*bubble sort*/
  19.    /*loop to control number of passes*/
  20.    for (pass = 1; pass < SIZE; pass++ ) {
  21.  
  22.       /*loop to control number of passes*/
  23.       for (i = 0; i< SIZE-1; I++) {
  24.  
  25.          if (a[i] > a[i +1] ) {
  26.  
  27.             hold = a[i] ;
  28.             a[i] = a[i+1];
  29.             a[i+1] = hold;
  30.          } 
  31.       }
  32.    }
  33.    printf( ''Data items in ascending order\n'') ;
  34.  
  35.    /*output sorted array*/
  36.    for ( i=0; i < SIZE; i++) {
  37.       printf( ''%d'', a[i] ) ;
  38.    }
  39.  
  40.    printf( ''\n'') ; 
  41.    return 0;
  42. }
Feb 17 '07 #1
3 1746
Ganon11
3,652 Expert 2GB
Your inner loop should somehow depend on your outer loop. For instance, the starting value of pass could somehow depend on i, or the end condition. This, as the outer loop continues, the inner loop's duration will get shorter and shorter.
Feb 17 '07 #2
willakawill
1,646 1GB
This notion does not work in bubble sort which is why it is so inefficient.
If the first 3 numbers are 3,2,1
after the first pass at i = 0
2,3,1
at i = 1
2,1,3

If we now start the second pass with i > 0 we mess up the sort because the first number remains at 2.
Feb 17 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
Your outer loop should start at the last position in the array and end at the first position and your inner loop should start at the first position and end at the current position of the outer loop.

Mary
Feb 19 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Gram | last post by:
Hello, Can anyone help out with a bubble sort for strings using ASP? I have a text file of words id like to sort and count. Thanks in advance for any help. Gram.
34
by: Mark Kamoski | last post by:
Hi-- Please help. I need a code sample for bubble sort. Thank you. --Mark
4
by: Chris | last post by:
I have a bubble sort for a 2-dimensional array that sorts a string,number pair based on the number. The code for the sort is as follows: Private Sub SortArray(ByRef roundarray(,) As String)...
0
by: Slowjam | last post by:
How do I correct the program below to search all the anagrams in a given file. First, for each word, build another word (its key) with all its letters sorted. For instance, build "dorw" for...
1
by: guest | last post by:
I am doing a program for Intro to Computer Programming where we take an array of strings and we must sort them alphabetically. we are supposed to use a bubble sort, but i know the code if meant for...
12
by: midknight5 | last post by:
Hello everyone, I am familiar with a normal bubble sort when dealing with an array of number but I am unsure as how to implement a sort when I have an array that is filled with classes which hold...
14
by: xtheendx | last post by:
I am writing a gradbook type program. It first allows the user to enter the number of students they want to enter. then allows them to enter the first name, last name, and grade of each student. The...
7
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.