Is there a way to improve the below Selection Sort code to make it more efficient?

int a, b, smallest, temp;
      for(a = 0; a < numIntegers - 1; a++)
      {
         smallest = a;;
         numComp++;
         for(b = a+1; b < numIntegers; b++)
         {
            if(Integer.parseInt(list[b].toString()) < Integer.parseInt(list[smallest].toString()))
            {
               smallest = b;
            }
            temp = Integer.parseInt(list[a].toString());
            list[a] = list[smallest];
            list[smallest] = temp;
            numSwaps++;
         }
      }

Recommended Answers

Well, for starters... If list is an array of integers, don't do...

Integer.parseInt(list[b].toString())
Jump to Post

All 2 Replies

Member Avatar

Well, for starters... If list is an array of integers, don't do...

Integer.parseInt(list[b].toString())

I mean in terms of the number of times it checks the if statement or the number of times it goes thru the for loops or swaps the items

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.