Java Program for Gnome Sort



Gnome Sort, also known as Stupid Sort, is a simple sorting algorithm that works by iterating through a list, comparing adjacent elements, and swapping them if they are in the wrong order. If a swap occurs, the algorithm moves one step backward to recheck the order, otherwise, it moves forward. This process continues until the array is sorted. In this article, we will explain the working of Gnome Sort using Java.

Gnome Sort Approach

  • Start from the first element.
  • Compare the current element with the previous one:
    • If they are in the correct order, move forward.
    • If they are not in the correct order, swap them and move backward.
  • Continue this process until the end of the array is reached.
  • The array is now sorted.

Java Program for Gnome Sort

Gnome Sort works with one element at a time and gets it to the actual position

Forward Movement When Order Is Correct
If the current element (arr[index]) is greater than or equal to the previous element (arr[index - 1]) or the index is at the start of the array, move the index forward.

if (index == 0 || arr[index] >= arr[index - 1]) {
index++;
}

Backward Movement When Order Is Incorrect
If the current element is smaller than the previous element, swap them and move the index backward to recheck the order of earlier elements.

int temp = arr[index];
arr[index] = arr[index - 1];
arr[index - 1] = temp;
Index--;

Termination of the Loop
The while loop runs until the index reaches the end of the array (index < arr.length). At this point, all elements are sorted.

while (index < arr.length) {
// Sorting logic here
}

Example

Let us see an example to implement Gnome Sort ?

public class GnomeSort {

    // Method to perform Gnome Sort
    public static void gnomeSort(int[] arr) {
        int index = 0;

        while (index < arr.length) {
            // If we are at the beginning or the elements are in correct order, move forward
            if (index == 0 || arr[index] >= arr[index - 1]) {
                index++;
            } else {
                // If elements are in the wrong order, swap them and move backward
                int temp = arr[index];
                arr[index] = arr[index - 1];
                arr[index - 1] = temp;
                index--;
            }
        }
    }

    // Helper method to print the array
    public static void printArray(int[] arr) {
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

    // Main method to test the Gnome Sort
    public static void main(String[] args) {
        int[] arr = {34, 2, 78, 12, 45, 1, 90};

        System.out.println("Original Array:");
        printArray(arr);

        gnomeSort(arr);

        System.out.println("Sorted Array:");
        printArray(arr);
    }
}

Output

Original Array:
34 2 78 12 45 1 90
Sorted Array:
1 2 12 34 45 78 90

Time ComplexityO(n^2) (similar to Bubble Sort).
Space Complexity: O(1) (in-place sorting).

Conclusion

Gnome Sort is a straightforward sorting algorithm that works well for small or nearly sorted datasets. While its performance is not optimal compared to more advanced algorithms, it offers a simple and educational introduction to sorting concepts in computer science.
Updated on: 2024-12-04T22:05:31+05:30

352 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements