Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.*;
  2. public class BubbleSort {
  3.     static void bubbleSort(int array []){
  4.         int n = array.length ;
  5.         int temp = 0;  
  6.         for(int i=0; i < n; i++){  
  7.                 for(int j=1; j < (n-i); j++){  
  8.                          if(array[j-1] > array[j]){  
  9.                                 //swap elements  
  10.                                 temp = array[j-1];  
  11.                                 array[j-1] = array[j];  
  12.                                 array[j] = temp;  
  13.                         }  
  14.                          
  15.                 }  
  16.         }
  17.  
  18.     }
  19. }
  20. ******************************************************************************************
  21. public class SelectionSort {
  22.     public static void selectionSort(int[] array){  
  23.         for (int i = 0; i < array.length - 1; i++)  
  24.         {  
  25.             int index = i;  
  26.             for (int j = i + 1; j < array.length; j++){  
  27.                 if (array[j] < array[index]){  
  28.                     index = j;//searching for lowest index  
  29.                 }  
  30.             }  
  31.             int smallerNumber = array[index];  
  32.             array[index] = array[i];  
  33.             array[i] = smallerNumber;  
  34.         }  
  35.     }  
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement