📜  改进线性搜索技术

📅  最后修改于: 2021-07-02 13:48:08             🧑  作者: Mango

线性搜索或顺序搜索是一种用于在列表中查找元素的方法。它顺序检查列表中的每个元素,直到找到匹配项或搜索了整个列表。可以观察到,当搜索关键元素时,就有可能一次又一次地搜索相同的关键元素。

目的是如果再次搜索相同的元素,则该操作必须花费更少的时间。因此,在这种情况下,可以使用以下两种方法来改进线性搜索:

  1. 换位
  2. 移到最前面

换位

在换位中,如果找到了key元素,则在增加特定key的搜索次数之前,会将它交换给元素一个索引,搜索操作也会优化并保持将元素移到数组的开始位置,搜索时间的复杂度将是恒定时间。

例如:如果数组arr []为{ 2,5,7,1,6,4,5,5,8,3,7 },并且要搜索的键为4,则以下步骤为:

  • 搜索键4之后,经过6次比较,该元素位于给定数组的索引5处。现在,在转置之后,数组变为{2,5,7,1,4,6,5,5,8,3,7},即,值为4的键位于索引4处。
  • 再次搜索键4之后,经过6次比较,该元素位于给定数组的索引4处。现在,在转置之后,数组变为{2,5,7,4,1,6,5,8,8,3,7},即,值为4的键位于索引3处。
  • 如果要查找的元素不在第一个索引处,则上述过程将继续进行,直到任何键到达数组的开头为止。

下面是上面讨论的算法的实现:

C
// C program for transposition to
// improve the Linear Search Algorithm
#include 
 
// Structure of the array
struct Array {
 
    int A[10];
    int size;
    int length;
};
 
// Function to print the array element
void Display(struct Array arr)
{
    int i;
 
    // Traverse the array arr[]
    for (i = 0; i < arr.length; i++) {
        printf("%d ", arr.A[i]);
    }
    printf("\n");
}
 
// Function that swaps two elements
// at different addresses
void swap(int* x, int* y)
{
    // Store the value store at
    // x in a variable temp
    int temp = *x;
 
    // Swapping of value
    *x = *y;
    *y = temp;
}
 
// Function that performs the Linear
// Search Transposition
int LinearSearchTransposition(
    struct Array* arr, int key)
{
    int i;
 
    // Traverse the array
    for (i = 0; i < arr->length; i++) {
 
        // If key is found, then swap
        // the element with it's
        // previous index
        if (key == arr->A[i]) {
 
            // If key is first element
            if (i == 0)
                return i;
 
            swap(&arr->A[i],
                 &arr->A[i - 1]);
 
            return i;
        }
    }
    return -1;
}
 
// Driver Code
int main()
{
    // Given array arr[]
    struct Array arr
        = { { 2, 23, 14, 5, 6, 9, 8, 12 },
            10,
            8 };
 
    printf("Elements before Linear"
           " Search Transposition: ");
 
    // Function Call for displaying
    // the array arr[]
    Display(arr);
 
    // Function Call for transposition
    LinearSearchTransposition(&arr, 14);
 
    printf("Elements after Linear"
           " Search Transposition: ");
 
    // Function Call for displaying
    // the array arr[]
    Display(arr);
    return 0;
}


Java
// Java program for transposition
// to improve the Linear Search
// Algorithm
class GFG{
 
// Structure of the
// array
static class Array
{
  int []A = new int[10];
  int size;
  int length;
  Array(int []A, int size,
        int length)
  {
    this.A = A;
    this.size = size;
    this.length = length;
  }
};
 
// Function to print the
// array element
static void Display(Array arr)
{
  int i;
 
  // Traverse the array arr[]
  for (i = 0; i < arr.length; i++)
  {
    System.out.printf("%d ",
                      arr.A[i]);
  }
  System.out.printf("\n");
}
 
// Function that performs the Linear
// Search Transposition
static int LinearSearchTransposition(Array arr,
                                     int key)
{
  int i;
 
  // Traverse the array
  for (i = 0; i < arr.length; i++)
  {
    // If key is found, then swap
    // the element with it's
    // previous index
    if (key == arr.A[i])
    {
      // If key is first element
      if (i == 0)
        return i;
      int temp = arr.A[i];
      arr.A[i] = arr.A[i - 1];
      arr.A[i - 1] = temp;
      return i;
    }
  }
  return -1;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given array arr[]
  int tempArr[] = {2, 23, 14, 5,
                   6, 9, 8, 12};
  Array arr = new Array(tempArr,
                        10, 8);
 
  System.out.printf("Elements before Linear" +
                    " Search Transposition: ");
 
  // Function Call for displaying
  // the array arr[]
  Display(arr);
 
  // Function Call for transposition
  LinearSearchTransposition(arr, 14);
 
  System.out.printf("Elements after Linear" +
                    " Search Transposition: ");
 
  // Function Call for displaying
  // the array arr[]
  Display(arr);
}
}
 
// This code is contributed by Princi Singh


C#
// C# program for transposition
// to improve the Linear Search
// Algorithm
using System;
 
class GFG{
 
// Structure of the
// array
public class Array
{
    public int []A = new int[10];
    public int size;
    public int length;
     
    public Array(int []A, int size,
                 int length)
    {
        this.A = A;
        this.size = size;
        this.length = length;
    }
};
 
// Function to print the
// array element
static void Display(Array arr)
{
    int i;
     
    // Traverse the array []arr
    for(i = 0; i < arr.length; i++)
    {
        Console.Write(arr.A[i] + " ");
    }
    Console.Write("\n");
}
 
// Function that performs the Linear
// Search Transposition
static int LinearSearchTransposition(Array arr,
                                     int key)
{
    int i;
     
    // Traverse the array
    for(i = 0; i < arr.length; i++)
    {
         
        // If key is found, then swap
        // the element with it's
        // previous index
        if (key == arr.A[i])
        {
             
            // If key is first element
            if (i == 0)
                return i;
                 
            int temp = arr.A[i];
            arr.A[i] = arr.A[i - 1];
            arr.A[i - 1] = temp;
            return i;
        }
    }
    return -1;
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given array []arr
    int []tempArr = { 2, 23, 14, 5,
                      6, 9, 8, 12 };
    Array arr = new Array(tempArr, 10, 8);
     
    Console.Write("Elements before Linear " +
                  "Search Transposition: ");
     
    // Function Call for displaying
    // the array []arr
    Display(arr);
     
    // Function Call for transposition
    LinearSearchTransposition(arr, 14);
     
    Console.Write("Elements after Linear " +
                  "Search Transposition: ");
     
    // Function Call for displaying
    // the array []arr
    Display(arr);
}
}
 
// This code is contributed by Amit Katiyar


C
// C program to implement the move to
// front to optimized Linear Search
#include 
 
// Structure of the array
struct Array {
 
    int A[10];
    int size;
    int length;
};
 
// Function to print the array element
void Display(struct Array arr)
{
    int i;
 
    // Traverse the array arr[]
    for (i = 0; i < arr.length; i++) {
        printf("%d ", arr.A[i]);
    }
    printf("\n");
}
 
// Function that swaps two elements
// at different addresses
void swap(int* x, int* y)
{
    // Store the value store at
    // x in a variable temp
    int temp = *x;
 
    // Swapping of value
    *x = *y;
    *y = temp;
}
 
// Function that performs the move to
// front operation for Linear Search
int LinearSearchMoveToFront(
    struct Array* arr, int key)
{
    int i;
 
    // Traverse the array
    for (i = 0; i < arr->length; i++) {
 
        // If key is found, then swap
        // the element with 0-th index
        if (key == arr->A[i]) {
            swap(&arr->A[i], &arr->A[0]);
            return i;
        }
    }
    return -1;
}
 
// Driver code
int main()
{
    // Given array arr[]
    struct Array arr
        = { { 2, 23, 14, 5, 6, 9, 8, 12 },
            10,
            8 };
 
    printf("Elements before Linear"
           " Search Move To Front: ");
 
    // Function Call for displaying
    // the array arr[]
    Display(arr);
 
    // Function Call for Move to
    // front operation
    LinearSearchMoveToFront(&arr, 14);
 
    printf("Elements after Linear"
           " Search Move To Front: ");
 
    // Function Call for displaying
    // the array arr[]
    Display(arr);
    return 0;
}


Java
// Java program to implement
// the move to front to optimized
// Linear Search
class GFG{
 
// Structure of the array
static class Array
{
  int []A = new int[10];
  int size;
  int length;
  Array(int []A, int size,
        int length)
  {
    this.A = A;
    this.size = size;
    this.length = length;
  }
};
 
// Function to print the
// array element
static void Display(Array arr)
{
  int i;
 
  // Traverse the array arr[]
  for (i = 0;
       i < arr.length; i++)
  {
    System.out.printf("%d ", arr.A[i]);
  }
  System.out.printf("\n");
}
 
// Function that performs the
// move to front operation for
// Linear Search
static int LinearSearchMoveToFront(Array arr,
                                   int key)
{
  int i;
 
  // Traverse the array
  for (i = 0; i < arr.length; i++)
  {
    // If key is found, then swap
    // the element with 0-th index
    if (key == arr.A[i])
    {
      int temp = arr.A[i];
      arr.A[i] = arr.A[0];
      arr.A[0] = temp;
      return i;
    }
  }
  return -1;
}
 
// Driver code
public static void main(String[] args)
{
  // Given array arr[]
  int a[] = {2, 23, 14, 5,
             6, 9, 8, 12 };
  Array arr = new Array(a, 10, 8);
 
  System.out.printf("Elements before Linear" +
                    " Search Move To Front: ");
 
  // Function Call for
  // displaying the array
  // arr[]
  Display(arr);
 
  // Function Call for Move
  // to front operation
  LinearSearchMoveToFront(arr, 14);
 
  System.out.printf("Elements after Linear" +
                    " Search Move To Front: ");
 
  // Function Call for displaying
  // the array arr[]
  Display(arr);
}
}
 
// This code is contributed by gauravrajput1


C#
// C# program to implement
// the move to front to optimized
// Linear Search
using System;
class GFG{
 
// Structure of the array
public class Array
{
  public int []A = new int[10];
  public int size;
  public int length;
  public Array(int []A,
               int size,
               int length)
  {
    this.A = A;
    this.size = size;
    this.length = length;
  }
};
 
// Function to print the
// array element
static void Display(Array arr)
{
  int i;
 
  // Traverse the array []arr
  for (i = 0;
       i < arr.length; i++)
  {
    Console.Write(" " + arr.A[i]);
  }
  Console.Write("\n");
}
 
// Function that performs the
// move to front operation for
// Linear Search
static int LinearSearchMoveToFront(Array arr,
                                   int key)
{
  int i;
 
  // Traverse the array
  for (i = 0; i < arr.length; i++)
  {
    // If key is found, then swap
    // the element with 0-th index
    if (key == arr.A[i])
    {
      int temp = arr.A[i];
      arr.A[i] = arr.A[0];
      arr.A[0] = temp;
      return i;
    }
  }
  return -1;
}
 
// Driver code
public static void Main(String[] args)
{
  // Given array []arr
  int []a = {2, 23, 14, 5,
             6, 9, 8, 12 };
   
  Array arr = new Array(a, 10, 8);
  Console.Write("Elements before Linear" +
                " Search Move To Front: ");
 
  // Function Call for
  // displaying the array
  // []arr
  Display(arr);
 
  // Function Call for Move
  // to front operation
  LinearSearchMoveToFront(arr, 14);
 
  Console.Write("Elements after Linear" +
                " Search Move To Front: ");
 
  // Function Call for displaying
  // the array []arr
  Display(arr);
}
}
 
// This code is contributed by gauravrajput1


输出:
线性搜索换位之前的元素:2 23 14 5 6 9 8 12
线性搜索换位后的元素:2 14 23 5 6 9 8 12

移至前/头

在此方法中,如果找到关键元素,则将其直接与索引0交换,以便在下一个连续时间中,对同一关键元素的搜索操作为O(1) ,即恒定时间。

例如:如果数组arr []为{ 2,5,7,1,6,4,5,5,8,3,7 },并且要搜索的键为4,那么以下步骤是:

  • 搜索键4之后,经过6次比较,该元素位于给定数组的索引5处。现在,移至前端操作后,数组变为{4,2,5,7,1,6,5,5,8,3,7},即,值为4的键位于索引0处
  • 再次搜索键4之后,在给定数组的索引0处找到了元素,这减少了整个元素的搜索空间。

C

// C program to implement the move to
// front to optimized Linear Search
#include 
 
// Structure of the array
struct Array {
 
    int A[10];
    int size;
    int length;
};
 
// Function to print the array element
void Display(struct Array arr)
{
    int i;
 
    // Traverse the array arr[]
    for (i = 0; i < arr.length; i++) {
        printf("%d ", arr.A[i]);
    }
    printf("\n");
}
 
// Function that swaps two elements
// at different addresses
void swap(int* x, int* y)
{
    // Store the value store at
    // x in a variable temp
    int temp = *x;
 
    // Swapping of value
    *x = *y;
    *y = temp;
}
 
// Function that performs the move to
// front operation for Linear Search
int LinearSearchMoveToFront(
    struct Array* arr, int key)
{
    int i;
 
    // Traverse the array
    for (i = 0; i < arr->length; i++) {
 
        // If key is found, then swap
        // the element with 0-th index
        if (key == arr->A[i]) {
            swap(&arr->A[i], &arr->A[0]);
            return i;
        }
    }
    return -1;
}
 
// Driver code
int main()
{
    // Given array arr[]
    struct Array arr
        = { { 2, 23, 14, 5, 6, 9, 8, 12 },
            10,
            8 };
 
    printf("Elements before Linear"
           " Search Move To Front: ");
 
    // Function Call for displaying
    // the array arr[]
    Display(arr);
 
    // Function Call for Move to
    // front operation
    LinearSearchMoveToFront(&arr, 14);
 
    printf("Elements after Linear"
           " Search Move To Front: ");
 
    // Function Call for displaying
    // the array arr[]
    Display(arr);
    return 0;
}

Java

// Java program to implement
// the move to front to optimized
// Linear Search
class GFG{
 
// Structure of the array
static class Array
{
  int []A = new int[10];
  int size;
  int length;
  Array(int []A, int size,
        int length)
  {
    this.A = A;
    this.size = size;
    this.length = length;
  }
};
 
// Function to print the
// array element
static void Display(Array arr)
{
  int i;
 
  // Traverse the array arr[]
  for (i = 0;
       i < arr.length; i++)
  {
    System.out.printf("%d ", arr.A[i]);
  }
  System.out.printf("\n");
}
 
// Function that performs the
// move to front operation for
// Linear Search
static int LinearSearchMoveToFront(Array arr,
                                   int key)
{
  int i;
 
  // Traverse the array
  for (i = 0; i < arr.length; i++)
  {
    // If key is found, then swap
    // the element with 0-th index
    if (key == arr.A[i])
    {
      int temp = arr.A[i];
      arr.A[i] = arr.A[0];
      arr.A[0] = temp;
      return i;
    }
  }
  return -1;
}
 
// Driver code
public static void main(String[] args)
{
  // Given array arr[]
  int a[] = {2, 23, 14, 5,
             6, 9, 8, 12 };
  Array arr = new Array(a, 10, 8);
 
  System.out.printf("Elements before Linear" +
                    " Search Move To Front: ");
 
  // Function Call for
  // displaying the array
  // arr[]
  Display(arr);
 
  // Function Call for Move
  // to front operation
  LinearSearchMoveToFront(arr, 14);
 
  System.out.printf("Elements after Linear" +
                    " Search Move To Front: ");
 
  // Function Call for displaying
  // the array arr[]
  Display(arr);
}
}
 
// This code is contributed by gauravrajput1

C#

// C# program to implement
// the move to front to optimized
// Linear Search
using System;
class GFG{
 
// Structure of the array
public class Array
{
  public int []A = new int[10];
  public int size;
  public int length;
  public Array(int []A,
               int size,
               int length)
  {
    this.A = A;
    this.size = size;
    this.length = length;
  }
};
 
// Function to print the
// array element
static void Display(Array arr)
{
  int i;
 
  // Traverse the array []arr
  for (i = 0;
       i < arr.length; i++)
  {
    Console.Write(" " + arr.A[i]);
  }
  Console.Write("\n");
}
 
// Function that performs the
// move to front operation for
// Linear Search
static int LinearSearchMoveToFront(Array arr,
                                   int key)
{
  int i;
 
  // Traverse the array
  for (i = 0; i < arr.length; i++)
  {
    // If key is found, then swap
    // the element with 0-th index
    if (key == arr.A[i])
    {
      int temp = arr.A[i];
      arr.A[i] = arr.A[0];
      arr.A[0] = temp;
      return i;
    }
  }
  return -1;
}
 
// Driver code
public static void Main(String[] args)
{
  // Given array []arr
  int []a = {2, 23, 14, 5,
             6, 9, 8, 12 };
   
  Array arr = new Array(a, 10, 8);
  Console.Write("Elements before Linear" +
                " Search Move To Front: ");
 
  // Function Call for
  // displaying the array
  // []arr
  Display(arr);
 
  // Function Call for Move
  // to front operation
  LinearSearchMoveToFront(arr, 14);
 
  Console.Write("Elements after Linear" +
                " Search Move To Front: ");
 
  // Function Call for displaying
  // the array []arr
  Display(arr);
}
}
 
// This code is contributed by gauravrajput1
输出:
线性搜索之前的元素移到最前面:2 23 14 5 6 9 8 12
线性搜索后的元素移至最前:14 23 2 5 6 9 8 12

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。