Java Program to Perform Searching Using Self-Organizing Lists

This is a java program to search an element using Self Organizing lists. A self-organizing list is a list that reorders its elements based on some self-organizing heuristic to improve average access time. The aim of a self-organizing list is to improve efficiency of linear search by moving more frequently accessed items towards the head of the list. A self-organizing list achieves near constant time for element access in the best case. A self-organizing list uses a reorganizing algorithm to adapt to various query distributions at runtime.

Here is the source code of the Java Program to Perform Searching Using Self-Organizing Lists. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

//This is a java program to search an element in self organizing lists
import java.util.Random;
import java.util.Scanner;
 
class SelfOrganizingList 
{
    private int[] list;
    private int[] count;
    private int size;
 
    public SelfOrganizingList(int listSize) 
    {
        list = new int[listSize];
        count = new int[listSize];
        size = 0;
    }
 
    public boolean isEmpty() 
    {
        return size == 0;
    }
 
    public boolean isFull() 
    {
        return size == list.length;
    }
 
    public void makeEmpty() 
    {
        int l = list.length;
        list = new int[l];
        count = new int[l];
        size = 0;
    }
 
    public int getSize() 
    {
        return size;
    }
 
    public void insert(int val) 
    {
        if (isFull()) 
        {
            System.out.println("Error : List full!");
            return;
        }
        list[size] = val;
        count[size] = 0;
        size++;
    }
 
    public void remove(int pos) 
    {
        pos--;
        if (pos < 0 || pos >= size) 
        {
            System.out.println("Invalid position ");
            return;
        }
        for (int i = pos; i < size - 1; i++) 
        {
            list[i] = list[i + 1];
            count[i] = count[i + 1];
        }
        size--;
    }
 
    public boolean search(int x) 
    {
        boolean searchResult = false;
        int pos = -1;
        for (int i = 0; i < size; i++) 
        {
            if (list[i] == x) {
                searchResult = true;
                pos = i;
                break;
            }
        }
        if (searchResult) 
        {
            count[pos]++;
            int c = count[pos];
            for (int i = 0; i < pos; i++) 
            {
                if (count[pos] > count[i]) 
                {
                    for (int j = pos; j > i; j--) 
                    {
                        list[j] = list[j - 1];
                        count[j] = count[j - 1];
                    }
                    list[i] = x;
                    count[i] = c;
                    break;
                }
            }
        }
        return searchResult;
    }
 
    public void printList() 
    {
        System.out.print("\nList = ");
        for (int i = 0; i < size; i++)
            System.out.print(list[i] + " ");
        System.out.print("\nCount = ");
        for (int i = 0; i < size; i++)
            System.out.print(count[i] + " ");
    }
}
 
public class Search_Self_Organizing 
{
    public static void main(String[] args) 
    {
        Random random = new Random();
        int N = 20;
 
        SelfOrganizingList list = new SelfOrganizingList(N);
        for (int i = 0; i < N; i++)
            list.insert(Math.abs(random.nextInt(1000)));
 
        Scanner scan = new Scanner(System.in);
        System.out.println("SelfOrganizingList Searching \n");
 
        list.printList();
 
        System.out.println("\nEnter integer element to search");
        System.out.println("Search Result : " + list.search(scan.nextInt()));
 
        scan.close();
    }
}

Output:

$ javac Search_Self_Organizing.java
$ java Search_Self_Organizing
 
SelfOrganizingList Searching 
 
List = 855 318 462 851 373 360 811 401 813 50 291 346 707 118 633 217 715 594 999 99 
Count = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
Enter integer element to search
811
Search Result : true

Related posts:

Removing all Nulls from a List in Java
Spring Security Remember Me
Java Program to Perform String Matching Using String Library
A Guide to Spring Boot Admin
Java Program to Check Whether an Undirected Graph Contains a Eulerian Path
Java Program to Implement Fermat Primality Test Algorithm
Properties with Spring and Spring Boot
Spring RestTemplate Request/Response Logging
Giới thiệu Java 8
Giới thiệu java.io.tmpdir
Java Program to Implement Weight Balanced Tree
Hướng dẫn Java Design Pattern – Iterator
Java Program to Generate a Graph for a Given Fixed Degree Sequence
Java Program to Implement Knapsack Algorithm
Java Program to Implement Gauss Jordan Elimination
Java Program to Implement the Alexander Bogomolny’s UnOrdered Permutation Algorithm for Elements Fro...
Filtering and Transforming Collections in Guava
Getting the Size of an Iterable in Java
Java Program to Implement SynchronosQueue API
Spring Boot - Rest Template
Guide to Dynamic Tests in Junit 5
Java Program to Find Transitive Closure of a Graph
Convert XML to JSON Using Jackson
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Java Program to Implement Splay Tree
Iterable to Stream in Java
Servlet 3 Async Support with Spring MVC and Spring Security
Hướng dẫn Java Design Pattern – Transfer Object
Collect a Java Stream to an Immutable Collection
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Java Program to Check if a Point d lies Inside or Outside a Circle Defined by Points a, b, c in a Pl...
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order