Back to feed
Renewal·서른의 생활코딩

Bubble Sort / Select Sort

NS
normalstory
cover image

package d_array;


public class Sortbasic {

public static void main(String[] args) {

/*

1. Sorting 

- Arranging data in a certain order so that specific data can be found quickly and easily

- For searching

- Bubble sort, selection sort, insertion sort

2. Bubble sort

- Sorting happens by continuously swapping 'adjacent data' items

- In ascending order, the largest value bubbles to the end. 

*/

// int[] array = new int[]{5,2,3,1,4};

// for(int i=0; i<5; i++){

// System.out.print(array[i] );

// if(array[0]>array[i]){

// array[i]=array[0];

// }

// }

// int[] array = new int[]{5,2,3,1,4};

// int max = array[0];

// for(int i=0; i<array.length;i++){

// if(array[0]<array[i]){

// array[0]=array[i];

// }

// System.out.print(array[i]);

// }

// int[] array = new int[]{5,2,3,1,4};

// int first = 5;

// if(array[1]>array[2]){

// array[1]=array[0];

// first=array[1];

// System.out.print(array[0]);

// }

// int[] array = new int[]{5,2,3,1,4};

////Round 1

// if(array[0]>array[1]){

// // swap the values

// int temp = array[0];

// array[0]=array[1];

// array[1]=temp;

// }

// if(array[1]>array[2]){

// // swap the values

// int temp = array[0];

// array[1]=array[2];

// array[2]=temp;

// }

// if(array[2]>array[3]){

// // swap the values

// int temp = array[0];

// array[0]=array[1];

// array[1]=temp;

// }

// if(array[3]>array[4]){

// // swap the values

// int temp = array[0];

// array[0]=array[1];

// array[1]=temp;

// }

// if(array[4]>array[i]){ // no more slot. 

// // swap the values

// }


// The comparison index doesn't reach the last slot

// The reference index is one less than the total size (runs up to index 1 less than length)

// Total length - 1

// int[] array = new int[]{5,2,3,1,4};

// for(int i=0; i<array.length-1;i++){ // i is the reference slot index

// if(array[i]>array[i+1]){

// // swap the values

// int temp = array[i];

// array[i]=array[i+1];

// array[i+1]=temp;

// }

// System.out.print(array[i]);

// }

// int[] array = new int[]{5,2,3,1,4};

////Round 1

// for(int i=0; i<array.length-1;i++){

// if(array[i]>array[i+1]){

// // swap the values

// int temp = array[i];

// array[i]=array[i+1];

// array[i+1]=temp;

// }

// System.out.print(array[i]);

// for(int j=0; j<array.length;j++){

// System.out.print(array[j]);

// }

// System.out.println("--Round 1");

// }

////Round 2

// for(int i=1; i<array.length-1;i++){

// if(array[i]>array[i+1]){

// // swap the values

// int temp = array[i];

// array[i]=array[i+1];

// array[i+1]=temp;

// }

// System.out.print(array[i]);

// for(int j=0; j<array.length;j++){

// System.out.print(array[j]);

// }

// System.out.println("--Round 2");

// }

////Round 3

// System.out.println("");

// for(int i=2; i<array.length-1;i++){

// if(array[i]>array[i+1]){

// // swap the values

// int temp = array[i];

// array[i]=array[i+1];

// array[i+1]=temp;

// }

// System.out.print(array[i]);

// for(int j=0; j<array.length;j++){

// System.out.print(array[j]);

// }

// System.out.println("--Round 3");

// }

////Round 4

// System.out.println("");

// for(int i=3; i<array.length-1;i++){

// if(array[i]>array[i+1]){

// // swap the values

// int temp = array[i];

// array[i]=array[i+1];

// array[i+1]=temp;

// }

// System.out.print(array[i]);

// for(int j=0; j<array.length;j++){

// System.out.print(array[j]);

// }

// System.out.println("--Round 4");

// }


// int[] array = new int[]{5,2,3,1,4};

// System.out.println("");

// for(int j=0; j<array.length-1-j;j++){  

// -j = shrinks as more positions are locked in

// = when one bubbling pass (one for loop) completes

// System.out.println("--Round 4");

// for(int i=0; i<array.length-1;i++){

// if(array[i]>array[i+1]){

// // swap the values

// int temp = array[i];

// array[i]=array[i+1];

// array[i+1]=temp;

// }

// for(int p=0; p<array.length;p++){

// System.out.print(array[p]);

// }

// }

// }

// Final

// int[] array = new int[]{5,2,3,1,4};

//

// for(int j=0; j<array.length-1;j++){// number of rounds

// System.out.println(" --- "+j+" round");

// for(int i=0; i<array.length-1-j;i++){// number of bubbles

// if(array[i]>array[i+1]){

// // swap the values

// int temp = array[i];

// array[i]=array[i+1];

// array[i+1]=temp;

// }

// for(int p=0; p<array.length-1;p++){

// System.out.print(array[p]);

// }

// System.out.println();

// }

// }

/*select sort : minimum value + one more thing added */

// Move the smallest number to the very front

//53142

// int[] num = new int[]{5,3,1,4,2};

//

// int fst = num[0]; // back up before it's overwritten during the for(if(min = num[i];)) process of finding the minimum 

// int min = 0;

// int no =0;

// for(int i=1; i<num.length-1;i++){ // loop that pairs front and back in the search 

// if(num[0]>num[i]){ // minimum-finding step

// no = i; // loop count = slot index = minimum-finding step

// min = num[i]; // skip? back the min up into the min variable

// }

// num[0]=min;  // the slot we started at as the reference  // skip? write the min variable into the reference slot

// }

// num[no]=fst;// position! only the slot info was carried over. Compute outside the for

//

// System.out.println("fst> "+fst); //fst> 3

// System.out.println("min> "+min); //min> 1

// System.out.println("min room no> "+no); //min room no> 2

//

// System.out.println("");

// for(int p=0; p<num.length;p++){

// System.out.print(num[p]);

// }

// int[] num = new int[]{5,3,1,4,2};

//

// int fst = num[0]; //3. back up the value that gets erased when the min moves slots

// int no =0;

// for(int i=1; i<num.length-1;i++){ //1. find the min, move the slot (min)

// if(num[0]>num[i]){

// num[0] = num[i];

// no = i; //2. find the min slot index (min index = loop count)

// }

// }

// num[no]=fst; //3. move the backed-up (nearly-erased) value into the min slot to bring it back

int[] num = new int[]{5,3,1,4,2};

for(int j=0; j<num.length-1; j++){

int fst = num[j]; 

int no =j;

for(int i=1; i<num.length-1;j++){

if(num[0]>num[i]){

num[0] = num[i];

no = i;

}

}

num[no]=fst;

for(int p=0; p<num.length;p++){

System.out.print(num[p]);

}

}




}

}



This English version was translated by Claude.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.

More on the author's page

Keep reading

Renewal

Steadily, for the long haul, without burning out

Mar 31, 2026·9 min
Renewal

Tech-life balance

Feb 7, 2026·3 min
Renewal

Humanality, by Park Jeong-ryeol

Feb 7, 2026·11 min