ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP2117Searching Unsorted Arr…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP2117

    Data Structures

    Progress0 / 37 done
    Topics37
    1Abstract Data Types2Complexity Analysis3Big Oh Notation4Stacks (Linked Lists and Array Implementations)5Recursion and analyzing recursive algorithms6Divide and Conquer Algorithms7Sorting Algorithms8Selection Sort9Insertion Sort10Merge Sort11Quick Sort12Bubble Sort13Heap Sort14Shell Sort15Radix Sort16Bucket Sort17Queue18Dequeuer19Priority Queues (linked and array implementations of queues)20Linked List and Its Various Types21Sorted Linked List22Searching an Unsorted Array23Binary Search for Sorted Arrays24Hashing and Indexing25Open Addressing and Chaining26Trees and Tree Traversals27Binary Search Trees28Heaps29M-Way Trees30Balanced Trees31Graphs32Breadth-First Traversal33Depth-First Traversal34Topological Order35Shortest Path36Adjacency Matrix and Adjacency List Implementations37Memory Management and Garbage Collection
    3 min read
    Topic 22 of 37
    Topic Study Note Illustration
    22

    Searching an Unsorted Array

    Searching an unsorted array involves locating a specific element or determining whether it exists within the array. Since the elements are not organized in any specific order,...

    Searching an Unsorted Array

    Searching an unsorted array involves locating a specific element or determining whether it exists within the array. Since the elements are not organized in any specific order, the most common method of searching is through linear search.

    Key Characteristics of Linear Search

    1. Simplicity: The algorithm is straightforward to implement.
    2. No Precondition: Works on any array, regardless of the order of elements.
    3. Time Complexity: The worst-case time complexity is O(n)O(n)O(n), where nnn is the number of elements in the array. This is because, in the worst case, the algorithm may need to check every element.

    Linear Search Algorithm

    Here’s a step-by-step outline of how linear search works:

    1. Start from the first element of the array.
    2. Compare each element with the target value.
    3. If a match is found, return the index of the matching element.
    4. If the end of the array is reached without finding the target, return an indication that the target is not present (e.g., -1).

    Example Code

    Here’s a simple C++ implementation of linear search for an unsorted array:

    #include <iostream>
    using namespace std;
    
    // Function to perform linear search
    int linearSearch(int arr[], int size, int target) {
        for (int i = 0; i < size; i++) {
            if (arr[i] == target) {
                return i; // Return the index if found
            }
        }
        return -1; // Return -1 if the target is not found
    }
    
    int main() {
        int arr[] = {3, 5, 2, 9, 1, 7}; // Example unsorted array
        int size = sizeof(arr) / sizeof(arr[0]);
        int target = 9;
    
        int result = linearSearch(arr, size, target);
        if (result != -1) {
            cout << "Element found at index: " << result << endl; // Output: Element found at index: 3
        } else {
            cout << "Element not found." << endl; // Output if not found
        }
    
        return 0;
    }
    

    Explanation of the Code

    1. Function Definition: The linearSearch function takes an array, its size, and the target value as parameters.
    2. Loop Through Array: The function iterates through each element of the array.
    3. Comparison: Each element is compared to the target value. If a match is found, the index of that element is returned.
    4. Return Value: If the loop completes without finding the target, the function returns -1 to indicate that the target is not present.
    5. Main Function: The main function initializes an unsorted array, specifies a target value, and calls the linearSearch function. The result is then printed.

    Complexity Analysis

    • Time Complexity:
      • Best Case: O(1)O(1)O(1) — If the target element is at the first index.
      • Worst Case: O(n)O(n)O(n) — If the target is at the last index or not present.
    • Space Complexity: O(1)O(1)O(1) — The algorithm uses a constant amount of extra space for the index variable.

    Conclusion

    Searching an unsorted array using linear search is efficient for small arrays or when a simple implementation is needed. However, for larger datasets, other search algorithms (like binary search) require sorted arrays and can be significantly faster, operating in O(log⁡n)O(\log n)O(logn) time. When dealing with unsorted data, linear search remains a fundamental method due to its simplicity and directness.

    ← Previous topic 21
    Sorted Linked List
    Next topic 23 →
    Binary Search for Sorted Arrays

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    592words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Data Structures