Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. /* Author: Mohamed Elshenawy
  2. Date: 19/6/2017
  3. Insertion Sort Implementation in java
  4. */
  5. import java.util.Scanner;
  6.  
  7. public class InsertionSort {
  8.  
  9. private static Scanner sc;
  10.  
  11. public InsertionSort() {
  12. }
  13.  
  14. public final void Sort(int[] arr) {
  15. int len = arr.length, in, out;
  16. for (out = 1; out < len; out++) {
  17. int temp = arr[out];
  18. in = out;
  19. while (in > 0 && arr[in - 1] >= temp) {
  20. arr[in] = arr[in - 1];
  21. --in;
  22. }
  23. arr[in] = temp;
  24. }
  25. }
  26.  
  27. public static void main(String[] args) {
  28. sc = new Scanner(System.in);
  29. int n = sc.nextInt();
  30. int[] a = new int[n];
  31.  
  32. for (int i = 0; i < n; i++) {
  33. a[i] = sc.nextInt();
  34. }
  35. for (int i : a) {
  36. System.out.print(i + " ");
  37. }
  38. System.out.println("\n");
  39. InsertionSort iSort = new InsertionSort();
  40. iSort.Sort(a);
  41. for (int i : a) {
  42. System.out.print(i + " ");
  43. }
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement