Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Random;
- public class Sorting {
- public static void selectionSort ( int[] arr ) {
- for ( int i = 0; i < arr.length; i++ ) {
- int minIndex = i;
- for ( int j = i + 1; j < arr.length; j++ ) {
- if ( arr[j] < arr [ minIndex ] )
- minIndex = j;
- }
- System.out.println( "Minimo trovato: " + minIndex + " value: " + arr[ minIndex ] );
- int sw = arr [ i ];
- arr [ i ] = arr [ minIndex ];
- arr[ minIndex ] = sw;
- }
- }
- static void insertionSort(int arr[])
- {
- int n = arr.length;
- int i, key, j;
- for (i = 1; i < n; i++)
- {
- key = arr[i];
- j = i - 1;
- while ( j >= 0 && arr[j] > key )
- {
- arr[j + 1] = arr[j];
- j = j - 1;
- }
- arr[j + 1] = key;
- }
- }
- static int[] mergeSort ( int arr[ ] ) {
- if ( arr.length == 1 )
- return arr;
- int[] firstHalf = new int[ (arr.length)/ 2 ];
- int[] secondHalf = new int[ (arr.length + 1)/ 2 ];
- // (src, src-offset, dest, offset, count )
- System.arraycopy( arr, 0, firstHalf, 0, firstHalf.length );
- System.arraycopy( arr, (arr.length) / 2, secondHalf, 0, secondHalf.length);
- firstHalf = mergeSort ( firstHalf );
- secondHalf = mergeSort ( secondHalf );
- int[] result = merge ( firstHalf, secondHalf );
- return result;
- }
- public static int[] merge ( int[] firstHalf, int[] secondHalf ) {
- int[] merged = new int[ firstHalf.length + secondHalf.length ];
- int i = 0;
- int j = 0;
- int posM = 0;
- while ( i < firstHalf.length && j < secondHalf.length ) {
- if ( firstHalf[ i ] < secondHalf [ j ] )
- merged [ posM++ ] = firstHalf[ i ++ ];
- else
- merged [ posM++ ] = secondHalf[ j ++ ];
- }
- while ( i < firstHalf.length )
- merged[ posM++ ] = firstHalf [ i++ ];
- while ( j < secondHalf.length )
- merged[ posM++ ] = secondHalf [ j++ ];
- return merged;
- }
- public static ArrayList<Integer> quickSort ( ArrayList<Integer> arr ) {
- if ( arr.size() <= 1 )
- return arr;
- int pivot = arr.get( new Random().nextInt( arr.size() ) ); /// arr [ random from 0 to length ]
- int pivotCounter = 0;
- ArrayList<Integer> bigger = new ArrayList<Integer>();
- ArrayList<Integer> smaller = new ArrayList<Integer>();
- for ( int i = 0; i < arr.size(); i++ ) {
- if ( arr.get( i ) > pivot )
- bigger.add( arr.get( i ) );
- if ( arr.get( i ) < pivot )
- smaller.add( arr.get( i ) );
- if ( arr.get( i ) == pivot )
- pivotCounter ++;
- }
- bigger = quickSort ( bigger );
- smaller = quickSort ( smaller );
- ArrayList<Integer> result = new ArrayList<Integer>();
- for ( int i = 0; i < smaller.size(); i++ )
- result.add( smaller.get(i) );
- for ( int i = 0; i < pivotCounter; i++ )
- result.add( pivot );
- for ( int i = 0; i < bigger.size(); i++ )
- result.add( bigger.get(i) );
- return result;
- }
- public static int[] integerSort ( int [] arr ) {
- int max = arr [ 0 ];
- for ( int i = 0; i < arr.length; i++ )
- if ( arr [ i ] > max )
- max = arr [ i ];
- int [ ] counter = new int [ max + 1 ];
- for ( int i = 0; i < arr.length; i++ )
- counter [ arr [ i ] ] ++;
- int [] result = new int [ arr.length ];
- int resultIndex = 0;
- for ( int i = 0; i < counter.length; i++ ) {
- while ( counter [ i ] > 0 ) {
- result [ resultIndex ++ ] = i;
- counter [ i ] --;
- }
- }
- return result;
- }
- public static void main( String[] args ) {
- Integer[] v = { 31,41,59,26,41,68,-1 };
- int[] v1 = { 31,41,59,26,41,68,-1 };
- int[] v2 = { 31,41,59,26,41,31,10, 31,32,1, 2, 44 };
- int[] result = integerSort ( v2 );
- ArrayList<Integer> toSort = new ArrayList<Integer>(Arrays.asList(v));
- ArrayList<Integer> res = quickSort ( toSort );
- for ( int x : result )
- System.out.print( x + " " );
- System.out.print ( "[ " );
- for ( int x : v ) {
- System.out.print ( x + ", " );
- }
- System.out.print ( " ]\n");
- mergeSort ( v1 );
- System.out.print ( "[ " );
- for ( int x : v ) {
- System.out.print ( x + ", " );
- }
- System.out.print ( " ]");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement