Day 10: Searching and Sorting Data in COBOL Tables for Efficient Information Retrieval and Data Organization

 

1. Introduction: Why Searching and Sorting Are Vital in COBOL

In business programs, handling large amounts of data efficiently is crucial. Whether it’s locating a customer record, finding a product by ID, or organizing employee salaries in order — searching and sorting make these operations simple and powerful in COBOL.

While tables (from Day 9) help store repetitive data, combining them with SEARCH and SORT operations turns them into flexible data management tools. Searching allows instant record lookups within a table, while sorting organizes records in ascending or descending order, improving readability and data processing.

In COBOL, searches can be performed linearly (checking each entry one by one) or using binary search (which is faster but requires sorted data). Sorting, on the other hand, is managed using the SORT verb, which can handle even external files — making COBOL highly adaptable for data-intensive environments.


2. Linear and Binary Search in COBOL Tables

COBOL provides two main ways to search data in tables:

a) Linear Search (SEARCH)

Checks each element until a match is found.
Example:

DATA DIVISION. WORKING-STORAGE SECTION. 01 EMP-NAME-TABLE. 05 EMP-NAME OCCURS 5 TIMES PIC X(20). 01 I PIC 9 VALUE 1. 01 FOUND-SWITCH PIC X VALUE 'N'. 01 SEARCH-NAME PIC X(20). PROCEDURE DIVISION. DISPLAY "ENTER 5 EMPLOYEE NAMES:". PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5 ACCEPT EMP-NAME(I) END-PERFORM. DISPLAY "ENTER NAME TO SEARCH: ". ACCEPT SEARCH-NAME. SEARCH EMP-NAME AT END DISPLAY "EMPLOYEE NOT FOUND" WHEN EMP-NAME(I) = SEARCH-NAME MOVE 'Y' TO FOUND-SWITCH DISPLAY "EMPLOYEE FOUND AT POSITION " I END-SEARCH.

b) Binary Search (SEARCH ALL)

Used only for sorted tables, it’s much faster since it divides the search space in half each time.
Example:

SEARCH ALL EMP-NAME WHEN EMP-NAME(I) = "PRAVEEN" DISPLAY "EMPLOYEE FOUND!" END-SEARCH.

3. Sorting Data in COBOL Using the SORT Verb

Sorting allows you to organize records in ascending or descending order based on key fields. The SORT verb works with input and output procedures, giving you full control over how data is read, sorted, and written.

Example:

SORT EMP-FILE ON ASCENDING KEY EMP-ID USING UNSORTED-FILE GIVING SORTED-FILE.

You can also specify an INPUT PROCEDURE (to format data before sorting) and an OUTPUT PROCEDURE (to process sorted data).

Sorting is especially important in reports, summaries, and batch jobs, where COBOL systems must handle thousands of records efficiently.


🧠 Key Takeaways

  • Use SEARCH for linear searches (simple and flexible).

  • Use SEARCH ALL for binary searches (faster, requires sorted data).

  • Use SORT to organize large datasets efficiently.

  • Combining tables + search + sort allows COBOL programs to manage real-world data effectively.

  • Sorting and searching are essential in banking, HR, and insurance applications.


💻 Practice Task

  1. Create a COBOL program EmployeeSearchSort.cbl.

  2. Store 10 employee names in a table.

  3. Sort them alphabetically using SORT.

  4. Implement a search function (linear or binary) to find an employee by name.

  5. Display whether the name exists and its position in the table.



"This Content Sponsored by SBO Digital Marketing.

Mobile-Based Part-Time Job Opportunity by SBO!

Earn money online by doing simple content publishing and sharing tasks. Here's how:

  • Job Type: Mobile-based part-time work
  • Work Involves:
    • Content publishing
    • Content sharing on social media
  • Time Required: As little as 1 hour a day
  • Earnings: ₹300 or more daily
  • Requirements:
    • Active Facebook and Instagram account
    • Basic knowledge of using mobile and social media

For more details:

WhatsApp your Name and Qualification to 9994104160

a.Online Part Time Jobs from Home

b.Work from Home Jobs Without Investment

c.Freelance Jobs Online for Students

d.Mobile Based Online Jobs

e.Daily Payment Online Jobs

Keyword & Tag: #OnlinePartTimeJob #WorkFromHome #EarnMoneyOnline #PartTimeJob #jobs #jobalerts #withoutinvestmentjob"

Comments