Lecture 2 - Introduction to Data Structures PDF

Summary

This document is a lecture on Introduction to Data Structures, covering linear and nonlinear data structures, arrays, array operations like insertion and deletion, and searching algorithms. It includes C code examples for array operations.

Full Transcript

2 Data Structure & Algorithms Code: INSY 8321 MODULE of DATA STRUCTURE AND ALGORITHMS INSTRUCTOR: BYIRINGIRO ERIC TEL:0782427392 E-mail :[email protected] 9/16/2024...

2 Data Structure & Algorithms Code: INSY 8321 MODULE of DATA STRUCTURE AND ALGORITHMS INSTRUCTOR: BYIRINGIRO ERIC TEL:0782427392 E-mail :[email protected] 9/16/2024 2 INTRODUCTION TO DATA STRUCTURE “Data structure is a Specific way to store and organize data in a computer's memory so that these data can be used efficiently later. ” Categories of Data Structure ❑ The data structure can be sub divided into major types:  Linear Data Structure  Non-linear Data Structure Linear Data Structure ❑ A data structure is said to be linear if its elements combined to form any specific order. ❑ Techniques of representing linear structure within memory: 1. Provide the linear relationships among all the elements represented by means of linear memory location. E.g Arrays 2. Provide the linear relationship among all the elements represented by using the concept of pointers or links(Linked list). ❑ The common examples of linear data structure are: 1. Arrays 2. Queues 3. Stacks 4. Linked lists Non-linear Data Structure ❑This structure is mostly used for representing data that contains a hierarchical relationship among various elements. ❑Examples of Non-Linear Data Structures are listed below: 1. Graphs 2. Trees Array ❑Array is a container which can hold a fix number of items and these items should be of the same type. ▪ Element − Each item stored in an array is called an element. ▪ Index − Each location of an element in an array has a numerical index, which is used to identify the element. ❑C array declaration: Arrays Basic Operations ❑ Following are the basic operations supported by an array.  Traverse − print all the array elements one by one.  Insertion − Adds an element at the given index.  Deletion − Deletes an element at the given index.  Search − Searches an element using the given index or by the value.  Update − Updates an element at the given index. Traversal Operation ❑Following is the algorithm to traverse through all the elements present in a Linear Array ❑Algorthim: 1. Start 2. Initialize an Array of certain size and datatype. 3. Initialize another variable ‘i’ with 0. 4. Print the ith value in the array and increment i. 5. Repeat Step 4 until the end of the array is reached. 6. End stdio.h ❑Stdio.h is a c header library that contains printf and scanf ❑printf() function is used to print the “character, string, float, integer, octal and hexadecimal values” onto the output screen. ❑scanf is a function that reads formatted data from stdio (i.e, the standard input stream, which is usually the keyboard) and then writes the results into the arguments given. ❑Passing &a means you are passing the address of a to scanf() Format specifier ❑We use printf() function with %d format specifier to display the value of an integer variable. ❑float(floating point values) uses %f, char (single character values) uses %c, character strings (arrays of characters) use %s. C program to traverse an array Insertion Operation 26 ❑Insert operation is to insert one or more data elements into an array. Based on the requirement, a new element can be added at the beginning, end, or any given index of array. Algorithm START DECLARE array LA initialized with zeros DECLARE integer i PRINT "Array Before Insertion:" FOR i = 0 TO 2 (Loop through array indices) PRINT "LA[i] = LA[i]" (Print the index and the initial value of each array element, which is 0) END FOR PRINT "The array elements after insertion:" FOR i = 0 TO 2 (Loop through array indices again) SET LA[i] = i + 2 (Assign the value of i + 2 to each array element) PRINT "LA[i] = LA[i]" (Print the index and the updated value of each array element) END FOR RETURN 0 (Indicate successful execution) END C program to insert element in Array Output Activity ❑Create linear array LA of float numbers. User asked to enter the size of array and enter array elements. The program inserts new element according to the selected position in array and print updated array elements. Deletion Operation 26 ❑In this array operation, we delete an element from the particular index of an array. ❑Consider LA is a linear array with N elements and K is a positive integer such that K n THEN END IF END C program to Delete element in Array at a specific index C program to Delete element in Array at a specific index, cont’ Activity ❑ Create a C program with Switch case statement to implement the following Array operations: 1. Find the Maximum Element 2. Find the Sum of all Elements 3. Reverse an Array Elements 4. Find the Average of Array Elements ❑Create a C program that asks the user for the number of hours worked by 6 employees ,calculates and displays the total hours worked by all employees and total wages if wage per hour is 1000$. Searching element in array ❑This section discusses two algorithms for searching the contents of an array(linear and binary search). ❑The Linear Search ❑The linear search sometimes called a sequential search, it uses a loop to sequentially step through an array, starting with the first element. ❑It compares each element with the value being searched for, and stops when either the value is found or the end of the array is encountered. ❑Ifthe value being searched for is not in the array, the algorithm will unsuccessfully search to the end of the array. C program to search for an element in an array ❑Algorithm: 1. Define an array array and initialize it with some values. 2. Define the variable size to store the size of the array. 3. Specify the target element that we want to search for. 4. Use a for loop to iterate over each element of the array. 5. Inside the loop, check if the current element is equal to the target element. If it is, print the index at which the element is found and set the found flag to 1. 6. If the found flag is still 0 after the loop, it means the element was not found, so print a message indicating that. ❑The Binary Search 1. The binary search requires that the values in the array be sorted in order. 2. Instead of testing the array’s first element, this algorithm starts with the element in the middle. 3. If that element happens to contain the desired value, then the search is over. Otherwise, the value in the middle element is either greater than or less than the value being searched for. 4. If it is greater, then the desired value (if it is in the list) will be found somewhere in the first half of the array. 5. If it is less, then the desired value (again, if it is in the list) will be found somewhere in the last half of the array. C program to find an element in a sorted array using binary search ❑Algorithm: 1. Define a binary search function binarySearch that takes an array, its size, and the target element to search for. 2. Inside the binarySearch function, initialize low to 0 and high to size - 1, representing the indices of the array. 3. Use a while loop to perform the binary search until low is less than or equal to high. 4. Inside the loop, calculate the mid index and check if the target is found at the middle. If it is, return the index. 5. If the target is greater than the element at mid, ignore the left half of the array by updating low to mid + 1. 6. If the target is smaller than the element at mid, ignore the right half of the array by updating high to mid - 1. 7. If the target is not found after the loop, return -1. 8. In the main function, define an array array, its size, and the target element to search for (target). 9. Call the binarySearch function to perform the binary search and store the result in the index variable. 10. Check if the index is not equal to -1, indicating that the target is found, and print the appropriate message. Otherwise, print that the element is not found in the array. Activity ❑Create an array of 5, 12, 23, 45, 67, 78, 100. Implement binary search to return the index where the 45 is found.

Use Quizgecko on...
Browser
Browser