# Problem with Selection Sort?

**URL:** <https://discourse.processing.org/t/problem-with-selection-sort/23506>\
**Category:** Coding Questions\
**Created:** [August 26, 2020, 5:37pm UTC](https://discourse.processing.org/t/problem-with-selection-sort/23506 "2020-08-26T17:37:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brucef](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brucef](https://discourse.processing.org/u/Brucef)\
**Post date:** [August 26, 2020, 5:37pm UTC](https://discourse.processing.org/t/problem-with-selection-sort/23506/1 "2020-08-26T17:37:22Z")

</div>

The Selection Sort coded below does not sort correctly. The numbers 7,66,5,40,33,99 are entered into an array called nums[]. When the program is run they do not sort from low to high? Certain sequences will sort, such as 10,9,8,7,6,5 but not all random sequences of six integers.  
I have reviewed the code many time but can not solve the problem.  
Can some one help me out?  
Bruce

int temp = 0;

int[] nums = {7,66,5,40,33,99};

void setup() {  
size(1000,1000);  
background(255);  
textSize(50);  
fill(255,0,0);

for(int i = 0; i \< nums.length; i++){  
text("["+i+"]",100 + 100 \* i, 60);  
text(nums[i],100 + 100 \* i, 120);  
}  
}

void draw(){  
fill(0,0,255);  
for (int i = 0; i \< nums.length-1; i++) {  
// Find the minimum element in unsorted array  
int min = i;  
for (int j = i+1; j \< nums.length; j++) {  
if (nums[j] \< nums[min]) {  
min = j;

```
        // Swap the found minimum element with the first 
        // element 
        int temp = nums[min]; 
        nums[min] = nums[i]; 
        nums[i] = temp; 
    } 
} 
    }

```

fill(255,0,0);  
for(int i = 0; i \< nums.length; i++){

```
  text(nums[i], 200+100*i, 780);
  text("["+i+"]",200 + 100 * i, 700);

```

}  
noLoop();  
}

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [August 26, 2020, 6:12pm UTC](https://discourse.processing.org/t/problem-with-selection-sort/23506/2 "2020-08-26T18:12:19Z")

</div>

Hello,

Welcome to the forum.

Please format your code:  
[https://discourse.processing.org/faq#format-your-code](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [August 27, 2020, 7:32am UTC](https://discourse.processing.org/t/problem-with-selection-sort/23506/3 "2020-08-27T07:32:28Z")

</div>

Hi @Brucef,

Welcome to the forum!

Do you need to build the sort function yourself?  
If not, take a look at the Arrays.sort() method

> **[Arrays.sort() in Java with examples - GeeksforGeeks](https://www.geeksforgeeks.org/arrays-sort-in-java-with-examples/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
