Java Program to Perform Searching based on Locality of Reference

This is a java program to perform searching based on locality of refernce. The Locality of Reference principle says that if an element of a list is accessed it might also be accessed in near future. So we store it to the front of the list.

Here is the source code of the Java Program to Perform Searching Based on Locality of Reference. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. /*
  3.  * Follows recently accessed elements should be referred more so kept on top of
  4.  * the list
  5.  */
  6. package com.sanfoundry.combinatorial;
  7.  
  8. import java.util.LinkedList;
  9. import java.util.List;
  10. import java.util.Random;
  11. import java.util.Scanner;
  12.  
  13. public class LocalityBasedSearching
  14. {
  15.     public static void main(String[] args)
  16.     {
  17.         List<Integer> items = new LinkedList<Integer>();
  18.         Integer n = 10;
  19.         Scanner sc = new Scanner(System.in);
  20.         Random rand = new Random();
  21.         while (n > 0)
  22.         {
  23.             items.add(rand.nextInt(100));
  24.             n--;
  25.         }
  26.         System.out.println(items.toString());
  27.         boolean flag = true;
  28.         boolean found = false;
  29.         Integer numberofInstance;
  30.         while (flag == true)
  31.         {
  32.             numberofInstance = 0;
  33.             System.out.println("Enter the element to find: ");
  34.             Integer search = sc.nextInt();
  35.             for (int i = 0; i < items.size(); i++)
  36.             {
  37.                 if (items.get(i).equals(search))
  38.                 {
  39.                     found = true;
  40.                     System.out.println("Element found at index " + i
  41.                             + "\nReordering list...");
  42.                     // Integer temp = items.get(numberofInstance);
  43.                     // items.set(numberofInstance, search);
  44.                     items.add(numberofInstance, search);
  45.                     items.remove(i + 1);
  46.                     // items.set(i, temp);
  47.                     System.out.println("Reordered list: " + items.toString());
  48.                     numberofInstance++;
  49.                     // break;
  50.                 }
  51.             }
  52.             if (found == false)
  53.             {
  54.                 System.out.println("No such element found.");
  55.             }
  56.             System.out.println("Do you want to continue? <true>/<false>");
  57.             flag = sc.nextBoolean();
  58.         }
  59.         sc.close();
  60.     }
  61. }

Output:

$ javac LocalityBasedSearching.java
$ java LocalityBasedSearching
 
[52, 94, 58, 8, 78, 0, 30, 81, 16, 58]
Enter the element to find: 
8
Element found at index 3
Reordering list...
Reordered list: [8, 52, 94, 58, 78, 0, 30, 81, 16, 58]
Do you want to continue? <true>/<false>
true
Enter the element to find: 
58
Element found at index 3
Reordering list...
Reordered list: [58, 8, 52, 94, 78, 0, 30, 81, 16, 58]
Element found at index 9
Reordering list...
Reordered list: [58, 58, 8, 52, 94, 78, 0, 30, 81, 16]
Do you want to continue? <true>/<false>
true
Enter the element to find: 
94
Element found at index 4
Reordering list...
Reordered list: [94, 58, 58, 8, 52, 78, 0, 30, 81, 16]
Do you want to continue? <true>/<false>
false

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.