What is Linear Sort in Java ?

Answer : One of the simplest Sorting techniques is a linear sort…

What is Linear Sort in Java ?

  • One of the simplest Sorting techniques is a linear sort.
  • Linear sort is the selection of an element and keeping it in sorted order.
  • In linear sort, the strategy is to find the smallest number in the array and exchange it with the value in first position of array.
  • Now, find the second smallest element in the remainder of array and exchange it with a value in the second position, carry on till you have reached the end of array.
  • Now all the elements have been sorted in ascending order.
 Linear Sorting

Sample Code

[pastacode lang=”java” manual=”%2F%2F%20Java%20program%20for%20implementation%20of%20Selection%20Sort%20%0Aclass%20SelectionSort%20%0A%7B%20%0A%20%20%20%20void%20sort(int%20arr%5B%5D)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%20%0A%20%20%0A%20%20%20%20%20%20%20%20%2F%2F%20One%20by%20one%20move%20boundary%20of%20unsorted%20subarray%20%0A%20%20%20%20%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n-1%3B%20i%2B%2B)%20%0A%20%20%20%20%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Find%20the%20minimum%20element%20in%20unsorted%20array%20%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20min_idx%20%3D%20i%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(int%20j%20%3D%20i%2B1%3B%20j%20%3C%20n%3B%20j%2B%2B)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(arr%5Bj%5D%20%3C%20arr%5Bmin_idx%5D)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20min_idx%20%3D%20j%3B%20%0A%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Swap%20the%20found%20minimum%20element%20with%20the%20first%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20element%20%0A%20%20%20%20%20%20%20%20%20%20%20%20int%20temp%20%3D%20arr%5Bmin_idx%5D%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bmin_idx%5D%20%3D%20arr%5Bi%5D%3B%20%0A%20%20%20%20%20%20%20%20%20%20%20%20arr%5Bi%5D%20%3D%20temp%3B%20%0A%20%20%20%20%20%20%20%20%7D%20%0A%20%20%20%20%7D%20%0A%20%20%0A%20%20%20%20%2F%2F%20Prints%20the%20array%20%0A%20%20%20%20void%20printArray(int%20arr%5B%5D)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20int%20n%20%3D%20arr.length%3B%20%0A%20%20%20%20%20%20%20%20for%20(int%20i%3D0%3B%20i%3Cn%3B%20%2B%2Bi)%20%0A%20%20%20%20%20%20%20%20%20%20%20%20System.out.print(arr%5Bi%5D%2B%22%20%22)%3B%20%0A%20%20%20%20%20%20%20%20System.out.println()%3B%20%0A%20%20%20%20%7D%20%0A%20%20%0A%20%20%20%20%2F%2F%20Driver%20code%20to%20test%20above%20%0A%20%20%20%20public%20static%20void%20main(String%20args%5B%5D)%20%0A%20%20%20%20%7B%20%0A%20%20%20%20%20%20%20%20SelectionSort%20ob%20%3D%20new%20SelectionSort()%3B%20%0A%20%20%20%20%20%20%20%20int%20arr%5B%5D%20%3D%20%7B27%2C28%2C45%2C68%2C32%7D%3B%20%0A%20%20%20%20%20%20%20%20ob.sort(arr)%3B%20%0A%20%20%20%20%20%20%20%20System.out.println(%22Sorted%20array%22)%3B%20%0A%20%20%20%20%20%20%20%20ob.printArray(arr)%3B%20%0A%20%20%20%20%7D%20%0A%7D%20″ message=”” highlight=”” provider=”manual”/]

Output

Sorted array:
27 28 32 45 68 
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like