importjava.util.Random;publicclassDay32{publicstaticvoidprintSequence(int[]sorted_sequence){for(inti=0;i<sorted_sequence.length;i++)System.out.print(sorted_sequence[i]+" ");}publicstaticint[]shakerSort(int[]array){for(inti=0;i<array.length/2;i++){booleanswapped=false;for(intj=i;j<array.length-i-1;j++){if(array[j]<array[j+1]){inttmp=array[j];array[j]=array[j+1];array[j+1]=tmp;}}for(intj=array.length-2-i;j>i;j--){if(array[j]>array[j-1]){inttmp=array[j];array[j]=array[j-1];array[j-1]=tmp;swapped=true;}}if(!swapped)break;}returnarray;}publicstaticvoidmain(Stringargs[]){System.out.println("Sorting of a randomly generated array with shaker sort");Randomrandom=newRandom();intN=20;int[]sequence=newint[N];for(inti=0;i<N;i++)sequence[i]=Math.abs(random.nextInt(100));System.out.println("\nOriginal: ");printSequence(sequence);System.out.println("\nSorted: ");printSequence(shakerSort(sequence));}}
Top comments (0)
Subscribe
We're a place where coders share, stay up-to-date and grow their careers.
Top comments (0)