ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Data Structures
    COMP2117
    Progress0 / 37 topics
    Topics
    1. Abstract Data Types2. Complexity Analysis3. Big Oh Notation4. Stacks (Linked Lists and Array Implementations)5. Recursion and analyzing recursive algorithms6. Divide and Conquer Algorithms7. Sorting Algorithms8. Selection Sort9. Insertion Sort10. Merge Sort11. Quick Sort12. Bubble Sort13. Heap Sort14. Shell Sort15. Radix Sort16. Bucket Sort17. Queue18. Dequeuer19. Priority Queues (linked and array implementations of queues)20. Linked List and Its Various Types21. Sorted Linked List22. Searching an Unsorted Array23. Binary Search for Sorted Arrays24. Hashing and Indexing25. Open Addressing and Chaining26. Trees and Tree Traversals27. Binary Search Trees28. Heaps29. M-Way Trees30. Balanced Trees31. Graphs32. Breadth-First Traversal33. Depth-First Traversal34. Topological Order35. Shortest Path36. Adjacency Matrix and Adjacency List Implementations37. Memory Management and Garbage Collection
    COMP2117›Searching an Unsorted Array
    Data StructuresTopic 22 of 37

    Searching an Unsorted Array

    3 minread
    592words
    Beginnerlevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time3 min
      Word count592
      Code examples0
      DifficultyBeginner