






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive guide to fundamental data structures and algorithms in python, specifically designed for computer engineering students. It covers various search algorithms, including linear search, sentinel search, binary search, and fibonacci search, as well as sorting algorithms like selection sort, bubble sort, insertion sort, and shell sort. The document also includes practical examples and code implementations to illustrate the concepts and their applications.
Typology: Study Guides, Projects, Research
1 / 11
This page cannot be seen from the preview
Don't miss anything!







Computer Engineering (Savitribai Phule Pune University)
The Linsearch function takes an array arr and a value x to search for. It iterates through the array and returns the index of the first occurrence of x if found, or -1 if not found.
python def Linsearch(arr, x): for i in range(len(arr)): if arr[i] == x: return i return -
The Sentsearch function takes an array arr and a value x to search for. It starts at index 0 and iterates through the array until it finds the value x. If found, it returns the index, otherwise it returns -1.
python def Sentsearch(arr, x): l = len(arr) i = 0 while(arr[i] != x): i = i + 1 if(i != l): return i else: return -
The driver code prompts the user to enter the number of students and their roll numbers, which are stored in an array. It then presents a menu with options to perform Linear Search and Sentinel Search on the array.
i = 0 for i in range(n): print('\n Enter roll number: ') item = int(input()) array.append(item) print('The Roll Numbers of Students are ...\n') print(array) while(True): print('Main Menu') print('\n 1. Linear Search') print('\n 2. Sentinel Search') print('\n 3. Exit') choice = int(input()) if(choice == 1): print('\n Enter the roll number to search if student has attended the training program or not? ') key = int(input()) location = Linsearch(array, key) if(location != -1): print('Yes, the student attended the training program!!!') else: print('No, the student has not attended the training program!!!') elif(choice == 2): print('\n Enter the roll number to search if student has attended the training program or not? ') key = int(input()) location = Sentsearch(array, key) if(location != -1): print('Yes, the student attended the training program!!!') else: print('No, the student has not attended the training program!!!') else: print('Exitting') break ``` **Experiment 11: (b) Write a Python Program to** **store roll numbers of student in array who** **attended training program in sorted order.** **Write a function for searching whether** **particular student attended training program** **or not, using Binary Search and Fibonacci** **Search** ## Binary Search The Binsearch function takes a sorted array arr and a value KEY to search for. It uses a binary search algorithm to find the index of KEY in the array, or -1 if not found. python def Binsearch(arr, KEY): low = 0 high = len(arr) - 1 m = 0 while(low <= high): m = (low + high) // 2 if(KEY < arr[m]): high = m - 1 elif(KEY > arr[m]): low = m + 1 else: return m return - ## Fibonacci Search The FibSearch function takes a sorted array arr, a value key to search for, and the length of the array n. It uses the Fibonacci search technique to find the index of key in the array, or -1 if not found. python def FibSearch(arr, key, n): b = 0 a = 1 f = b + a while (f < n): b = a a = f f = b + a offset = -1 while (f > 1): i = min(offset + b, n - 1) if (arr[i] < key): f = a a = b b = f - a offset = i elif (arr[i] > key): f = b a = a - b b = f - a else: return i if(a and arr[offset + 1] == key): return offset + 1 return - arr[j+1] arr[j+1] = temp print(arr) for i in range (len(arr)-1, 1, -1): print(arr[i]) ## Driver Code The driver code prompts the user to enter the number of students and their first-year percentage marks, which are stored in an array. It then presents a menu with options to perform Selection Sort and Bubble Sort on the array. ```python print('\nHow many students are there?') n = int(input()) array = [] i = 0 for i in range(n): print('\n Enter percentage marks') item = float(input()) array.append(item) print('You have entered following scores... \n') print(array) while(True): print('Main Menu') print('\n 1. Selection Sort') print('\n 2. Bubble Sort') print('\n 3. Exit') choice = int(input()) if(choice == 1): print('\n The sorted Scores are...') SelectionSort(array, n) elif(choice == 2): print('\n The sorted Scores are...') BubbleSort(array, n) else: print('Exitting') break Experiment 13: Write a Python program to
store second year percentage of students in
array. Write a function for sorting array of
floating point numbers in ascending order
using (a) Insertion Sort and (b) Shell Sort,
and display top five scores
The InsertSort function takes an array arr and its length n. It performs the insertion sort algorithm to sort the array in ascending order, and then prints the top five scores.
python def InsertSort(arr, n): i = 1 for i in range(n): temp = arr[i] j = i - 1 while((j >= 0) & (arr[j] > temp)): arr[j + 1] = arr[j] j = j - 1 arr[j + 1] = temp print(arr) print('Top Five Scores are...') for i in range (len(arr) - 1, 1, -1): print(arr[i])
The ShellSort function takes an array arr and its length n. It performs the shell sort algorithm to sort the array in ascending order, and then prints the top five scores.
python def ShellSort(arr, n): d = n // 2 while d > 0: for i in range(d, n): temp = arr[i] j = i while(j >= d and arr[j-d] >
temp): arr[j] = arr[j-d] j -= d arr[j] = temp d = d // 2 print(arr) for i in range (len(arr) - 1, 1, -1): print(arr[i])
The driver code prompts the user to enter the number of students and their second-year percentage marks, which are stored in an array. It then presents a menu with options to perform Insertion Sort and Shell Sort on the array.
i = 0 for i in range(n): print('\n Enter percentage marks') item = float(input()) array.append(item) print('You have entered following scores... \n') print(array) while(True): print('Main Menu') print('\n 1. Insertion Sort') print('\n 2. Shell Sort') print('\n 3. Exit') choice = int(input()) if(choice == 1): print('\n The sorted Scores are...') InsertSort(array, n) elif(choice == 2): print('\n The sorted Scores are...') ShellSort(array, n) else: print('Exitting') break ``` **Maintaining Club Member Information using** **Singly Linked List** ## Introduction The Department of Computer Engineering has a student's club named 'Pinnacle Club'. Students of the second, third, and final year of the department can be granted membership on request. Similarly, one may cancel the membership of the club. The first node is reserved for the president of the club, and the last node is reserved for the secretary of the club. This document presents a C++ program to maintain the club member's information using a singly linked list. The program stores the student's PRN (Permanent Registration Number) and Name. ## Functions The program includes the following functions: **Add Member** : This function allows adding a new member to the club. The function takes the student's PRN and Name as input and adds the new node to the linked list. **Delete Member** : This function allows deleting a member from the club. The function takes the student's PRN as input and removes the corresponding node from the linked list. **Add President** : This function allows adding a new president to the club. The function takes the student's PRN and Name as input and adds the new node as the first node in the linked list. // Delete president deletePresident(&head); // Delete secretary deleteSecretary(&head); // Display members displayMembers(head); return 0; } ``` ## Conclusion The provided C++ program demonstrates the implementation of a singly linked list to maintain the club member's information. The program includes functions to add and delete members, as well as the president and secretary of the club. The displayMembers function allows viewing the current list of club members. **Palindrome Checker** ## Introduction A palindrome is a string of characters that reads the same forward and backward, ignoring punctuation, capitalization, and spaces. For example, "Poor Dan is in a droop" is a palindrome, as the sequence of characters "poordanisinadroop" is the same forward and backward. One way to check if a string is a palindrome is to reverse the characters in the string and then compare them to the original string. If the sequence is identical, then the string is a palindrome. ## Program Functionality The C++ program provided has the following functionality: **Print Original String and Reversed String using Stack** : The program uses a stack data structure to reverse the characters in the input string and then prints the original string followed by the reversed string. **Check if String is Palindrome** : The program checks if the given input string is a palindrome by comparing the original string with the reversed string. If they are identical, the program outputs that the string is a palindrome, otherwise, it outputs that the string is not a palindrome. ## Code Implementation The program includes the following functions: reverseString(char* str): This function takes a C-style string as input and uses a stack to reverse the characters in the string. It then prints the original string followed by the reversed string. isPalindrome(char* str): This function takes a C-style string as input and checks if the string is a palindrome by comparing the original string with the reversed string. It returns true if the string is a palindrome, and false otherwise. The main() function prompts the user to enter a string, calls the reverseString() function to print the original and reversed strings, and then calls the isPalindrome() function to check if the string is a palindrome. ## Example Output ``` Enter a string: Poor Dan is in a droop Original string: Poor Dan is in a droop Reversed string: pooRnaDsinanidroop The string is a palindrome. Enter a string: Hello World Original string: Hello World Reversed string: dlroWolleH The string is not a palindrome. ``` ## Conclusion The provided C++ program demonstrates the implementation of a palindrome checker using a stack data structure to reverse the input string and compare it with the original string. This approach allows for efficient checking of palindromes, as the time complexity of the reversal and comparison operations is linear. **Implementing a Stack in C++** ## Introduction The provided text describes the implementation of a Stack data structure in C++. A Stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle, where the last element added to the stack is the first one to be removed. ## Class Definition The Stack class is defined with the following private and public members: **Private Members:** - input_str[SIZE]: An array to store the input string. - stack_data[SIZE]: An array to store the elements of the stack. - top: An integer to keep track of the top of the stack. - length: An integer to store The program correctly identifies the input string "poor dan is in a droop" as a palindrome and displays the original and reversed strings. **Doubly Ended Queue using Arrays** ## Introduction The provided text describes the implementation of a Doubly Ended Queue (DeQue) data structure using arrays in C++. A Doubly Ended Queue is a linear data structure that allows insertion and deletion of elements from both the front and the rear ends. ## Data Structure The data structure for the DeQue is defined as follows: cpp struct queue { int que[size]; int front, rear; } Q; The queue structure contains an array que of size size, and two integer variables front and rear to keep track of the front and rear elements of the queue, respectively. ## DeQue Class The DeQue class is defined with the following member functions: DeQue(): The constructor initializes the front and rear variables to -1, and sets all the elements of the que array to -1. Qfull(): This function checks if the queue is full, i.e., if the rear index is equal to size - 1. Qempty(): This function checks if the queue is empty, i.e., if the front index is greater than the rear index, or if both front and rear are -1. insert_rear(int item): This function inserts an element at the rear end of the queue. delete_front(): This function deletes and returns the element at the front end of the queue. insert_front(int item): This function inserts an element at the front end of the queue. delete_rear(): This function deletes and returns the element at the rear end of the queue. display(): This function displays the elements of the queue. ## Main Function The main() function provides a menu-driven interface to interact with the DeQue. It allows the user to perform the following operations: Insert an element at the rear end of the queue. Delete an element from the front end of the queue. Insert an element at the front end of the queue. Delete an element from the rear end of the queue. Display the elements of the queue. Exit the program. The program continues to run until the user chooses to exit. ## Output The output of the program demonstrates the various operations performed on the DeQue, such as inserting and deleting elements from both the front and rear ends, and displaying the contents of the queue.