Sorting pixels with bubble sort
in
Programming Questions
•
1 year ago
Hello,
I'm new to sorting algorithms, and in an effort to keep things as simple as possible for myself, I decided to use a bubble sort to sort the pixels of an image. I know that bubble sort is going to be slow, but I'd like to make it work anyhow. When I run the code below nothing happens.
Is the problem in my sort, or does it have to do with the way I'm filling the array from the pixels?
Here is what I've got.
Here is what I've got.
- PImage img;
- int increment= 2;
- void setup(){
- img = loadImage("image01.png");
- size (img.width, img.height);
- image(img, 0, 0, width, height);
- }
- void doSort(){
- loadPixels();
- for (int x = 0; x<((width*height)-(increment+2)); x++){
- bubbleSort(pixels,x, x+increment);
- }
- updatePixels();
- }
- void bubbleSort (int x[], int i, int j){
- // loop through the array
- for (i = 0; i<x.length-1;i++) {
- if (x[i+1] > x[i]) {
- //swap i and j values
- j = x[i];
- x[i] = x[i+1];
- x[i+1] = j;
- }
- }
- }
1