473,402 Members | 2,064 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,402 software developers and data experts.

What is wrong with my function for Selection sort?

The function works fine if the "small" I have chosen, ie, a[i], is *not* the smallest element in the array. It swaps and sorts just fine. But when the "small", ie, a[i], is the smallest element in the array already, then it goes bonkers, still substituting it with the previous value of "pos".

How do I remedy this?

Here is the code I have written -

Expand|Select|Wrap|Line Numbers
  1. void selection(int a[], int size)
  2. {
  3.  int small, pos=0, temp, flag=0;
  4.  for(i=0;i<(size-1);i++)
  5.     {
  6.      small=a[i];
  7.      for(j=i+1;j<size;j++)
  8.         {
  9.          if(a[j]<small)
  10.             {
  11.              flag=1;
  12.              small=a[j];
  13.              pos=j;
  14.             }
  15.          else flag=0;
  16.         }
  17.      if(flag=1)
  18.         {
  19.          temp=a[i];
  20.          a[i]=a[pos];
  21.          a[pos]=temp;
  22.         }
  23.      cout<<"\nArray after Pass "<<i+1<<" is - ";
  24.      for(int k=0;k<size;k++)
  25.         cout<<a[k]<<" ";
  26.     }
  27.  cout<<"\n\nThe finished and sorted array is as follows...\n\n";
  28.  display(a,size);
  29.  return;
  30. }
Oct 7 '10 #1
5 2114
donbock
2,426 Expert 2GB
Line 17 should be "if(flag==1)".

I haven't taken the time to truly understand your code, but I'm suspicious of how flag is used in lines 7-16. Line 15 will reset the flag, causing the program to forget that it was set by an earlier pass through the loop.
Oct 7 '10 #2
Hi. I've made that change. I've turned it to "if(flag==1)". It is still showing the error.
Oct 7 '10 #3
Alex T
29
Can you elaborate on how your program works, using comments in your code?
Oct 8 '10 #4
newb16
687 512MB
You have the error in determining if the flag should be set. It is set only if the last element of the array is less than the smallest element found, else it is 0.
Oct 8 '10 #5
donbock
2,426 Expert 2GB
Please take a moment to describe the algorithm for Selection Sort using natural language (not computer language). Ideally, this description would go in your source code as comments.
Oct 8 '10 #6

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

Similar topics

5
by: Eric | last post by:
The following example will explain what i want to do: >>> def func(): print "true" >>> rules=(func,) >>> for rule in rules: rule I expect the final function to print true, but instead i have...
4
by: David Douglass | last post by:
I'm confused about the program below. Based on my reading of the C# spec, I don't think it should compile, but it does when using Beta 1. Could somebody please explain the function selection...
4
by: shaveta | last post by:
pls help me to write a program in c that reads n elements of type float, stores them in a linked list ,sort it in the descending order using selection sort methods, and finally output the sorted...
1
by: gemacjr1201 | last post by:
I need hints for an algorithm using string compare in a selection sort. I will be sorting lastnames and first namesalphbetically. if the selection sort encounters the same last name it then goes to...
1
by: ShaveDave27 | last post by:
Hi, I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday). Now...
12
by: ShaveDave27 | last post by:
HI, I've created a Comparable interface class Person. Which has variables First_name, surname, month(Of birth), day(Of birth) , and birthday(which i have created from month and day). I have a...
1
by: neyugncul | last post by:
Okay, I have this homework assignment which is to sort a 2D array of characters using selection sort. I've written the selection sort but I can't seem to get it working. (it's not sorting!) ...
1
by: fatimaballes | last post by:
how to apply bubble and selection sort in assembly?this is for my subject IT103Lab.. were using notepad++ for encoding then amke everything appear in the command prompt.. another thing... our...
1
mfshake
by: mfshake | last post by:
i am using only a driver and not a server. I know how to do selection sort for ints but can't figure out how to do it for Strings. I want to sort it my first letter only Here is my code that i...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.