473,395 Members | 1,441 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,395 software developers and data experts.

bubble sort ascending help?

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. main()
  6.  
  7. {
  8.     int ctr, inner, outer, didSwap, temp;
  9.     int nums[10];
  10.     time_t t;
  11.  
  12.     srand(time(&t));
  13.  
  14.     for (ctr = 0; ctr < 10; ctr++)
  15.     {
  16.         nums[ctr] = (rand() % 99) + 1;
  17.     }
  18. puts("\nHere is the list before the sort:");
  19. for (ctr=0; ctr < 10; ctr++)
  20. {
  21.     printf("%d\n", nums[ctr]);
  22. }
  23.  
  24. for(outer = 0; outer < 9; outer++)
  25. {
  26.     didSwap = 0;
  27.     for (inner = outer; inner < 10; inner++)
  28.     {
  29.         if (nums[inner] < nums[outer])
  30.         {
  31.            temp = nums[inner];
  32.            nums[inner] = nums[outer];
  33.            nums[outer] = temp;
  34.            didSwap = 1;
  35.         }
  36.     }
  37.     if (didSwap == 0)
  38.     {
  39.         break;
  40.     }
  41.  
  42. }
  43.  
  44. puts("\nHere is the list after the sort:");
  45. for(ctr = 0; ctr < 10; ctr++)
  46. {
  47.     printf("%d\n", nums[ctr]);
  48.  
  49. }
  50. return 0;
  51. }
  52.  

in this code i did not understand this part.
Expand|Select|Wrap|Line Numbers
  1. for(outer = 0; outer < 9; outer++)
  2. {
  3.     didSwap = 0;
  4.     for (inner = outer; inner < 10; inner++)
  5.     {
  6.         if (nums[inner] < nums[outer])
  7.  
  8.  
if outer = 0 and inner = outer then both inner and outer = 0. and if the loop FOR says
Expand|Select|Wrap|Line Numbers
  1. if (nums[inner] < nums[outer])
then how can nums[0] can be smaller than nums[0] since both inner and outer = 0. please help me to understand.
Nov 6 '13 #1
0 1108

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)...
2
by: joesindel | last post by:
So heres the deal... I work for a company in which numbers are dropping. The owners created a game where calling potential clients racks up points for crap like basketball ticekts and such. The...
5
by: poreko | last post by:
hi guys! i am doing a bubble sort on using the C language. I have to prompt the user to enter 10 integers values of his/her choice. Store these values into an array in the order the user entered...
3
by: biddy2 | last post by:
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...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.