CS1100 Computational Engineering

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
CHAPTER 10 ARRAYS II Applications and Extensions.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Chapter 8 Arrays and Strings
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
Chapter 6 Structures Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
Searching and Sorting Searching algorithms with simple arrays
Using recursion for Searching and Sorting
Sorts, CompareTo Method and Strings
C Structures and Memory Allocation
1-d Arrays.
Computer Organization and Design Pointers, Arrays and Strings in C
5.13 Recursion Recursive functions Functions that call themselves
CSC 427: Data Structures and Algorithm Analysis
Analysis of Algorithms
Introduction to Search Algorithms
Computer Programming BCT 1113
Recursion notes Chapter 8.
Chapter 9: Searching, Sorting, and Algorithm Analysis
Recitation 13 Searching and Sorting.
CS1010 Discussion Group 11 Week 11 – Recursion recursion recursion….
Learning Objectives Pointers Pointer in function call
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
CSE 5311 Advanced Algorithms
Divide and Conquer.
Advance Analysis of Algorithms
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Circular Buffers, Linked Lists
Using Arrays in C Only fixed-length arrays can be initialized when they are defined. Variable length arrays must be initialized by inputting or assigning.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
CS 3343: Analysis of Algorithms
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Algorithm design and Analysis
ARRAYS Lecture
Unit-2 Divide and Conquer
CS Two Basic Sorting Algorithms Review Exchange Sorting Merge Sorting
Data Structures Review Session
Searching and Sorting Arrays
CS1100 Computational Engineering
Search,Sort,Recursion.
CS111 Computer Programming
Structures CSE 2031 Fall February 2019.
CS150 Introduction to Computer Science 1
Search,Sort,Recursion.
CSC 427: Data Structures and Algorithm Analysis
CS150 Introduction to Computer Science 1
Recursion Chapter 11.
Data Structures and Algorithms Introduction to Pointers
Data Structures & Algorithms
Chapter 9: Pointers and String
Structures CSE 2031 Fall April 2019.
Structures CSE 2031 Fall May 2019.
Recursive Algorithms 1 Building a Ruler: drawRuler()
Structures EECS July 2019.
C Structures and Memory Allocation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

CS1100 Computational Engineering Searching and Structures Madhu Mutyam and Sukendu Das

Searching for Elements Given an array of marks and names, is there someone who got X marks? If X occurs in the Marks array, return the index of the position where it occurs. If the numbers are randomly arranged, there is no option but to scan the entire array to be sure. Would the task be simpler if the marks were arranged in sorted order? Like finding a word in a dictionary

Searching in a Sorted Array Given an array of marks and names sorted in descending order of marks, is there someone who got X marks? If X is high (say 92/100), one could start scanning from the left. If X is low (say 47/100), one could scan the array right to left. But what if we do not know whether X is high or low?

Look at the middle element If array[middle] = = X, done Divide and Conquer Largest Smallest Look at the middle element If array[middle] = = X, done If array[middle] > X, look only in the right part Else look for the number only in the left part The problem is reduced into a smaller problem new problem is half the size of the original one Recursively apply this strategy

middle = (left + right)/2 Divide and Conquer Two indexes define the range of searching If array[middle] > X look only in the right part left right middle = (left + right)/2 newLeft = middle+1 right newMiddle

Binary Search (also called Binary Chop) Starts with the full sorted array left = 0 and right = N-1 The range of search are the elements between left and right including array[left] and array[right] Search terminates if right < left Otherwise If (array[middle] == X) return middle If (array[middle] < X) left = middle +1 Else right = middle -1

Binary Search (bsearch in stdlib.h – elements increasing order) int BinarySearch(int value, int array[ ], int n){ int left = 0, right = n-1; while (left <= right){ middle = (left+right)/2; if (array[middle] == value) return middle; if (array[middle] < value) left = middle +1; else right = middle -1; } return INFINITY; /*calling function must interpret this correctly! */

Complexity of Binary Search After each inspection the array reduces by half. For an array of size N there are about log2N inspections in the worst case.

Complexity: Recurrence Equation Let n be the size of the array Let the function T(n) represent the time complexity of solving the problem of size n In each call the problem is reduced by half one comparison in each call T(n) = 1 + T(n/2) Solving this gives us the complexity log2(n) T(n) = 1 + T(n/2) T(n/2) = 1 + T(n/4) T(n/4) = 1 + T(n/8) . T(2) = 1 + T(1) = 1 + 1 n 128 256 512 1024 2048 4096 … log n 7 8 9 10 11 12 log(n) equations

Finding Student with X marks char *findStudent (int x, char names[][COL], int marks[], int n) { int index = BinarySearch(x, marks[], n); if (index == n) return “no such student”; else return names[index]; } Above function assumes that names are corresponding to marks, and marks are in decreasing order. return NULL; Questions : What happens if we call BinarySearch(X, Marks[], N)? Does names array has to have ‘\0’ to return a string? What happens when you don’t have nul in a character array? Is it still a string? question for strings Why is *(array+row) in *(*(array + row) + col) not dereference?

The connection between the two arrays was via the shared index Marks and Names We kept Marks in an integer array and Names in a corresponding two dimensional array could keep Names in an array of pointers memory reserved as per requirements The connection between the two arrays was via the shared index Can we keep them in the same array? one option – keep first three characters for marks and convert them while processing a better option – use structures

Structures Collection of one or more variables, possibly of different types, grouped together under a single name for easy handling. For example - a structure which represents a point in a two dimensional plane       struct point{             int x;             int y;       }; A mechanism for defining compound data types By itself it reserves no storage

Point in 2D  2 Integers Different ways of declaring structure variables struct point{             int x;             int y;       } point1, point2;  struct point point1, point2; struct point point1 = { 3 , 2 };

struct student s1 = { “Ramesh” , 79 }; Marks and Names struct student{             char *name;             int mark;        }s1, s2;  struct student s1, s2; struct student s1 = { “Ramesh” , 79 }; name could itself be a struct made up of first name, middle name and last name… Nested structures are allowed

• A Rectangle struct rectangle{ struct point pt1; struct point pt2; Accessing points in the rectangle       rect1.pt1.x = 4;       rect1.pt1.y = 5;             Or       rect1.pt1 = { 4, 5 }; •

Defining New Types ‘typedef’ keyword is used for creating new data types For example: typedef int Age; Age myAge = 99; typedef and Structures: typedef struct point pointType; pointType point1, point2;  This is equivalent to: struct point point1, point2; 

Operations on Structures Structures may be copied by assignment statement The address of the structure (use &) can be passed to functions and can be returned by functions one can pass an entire structure one can pass some components of a structure one can pass a pointer to a structure Structures may not be compared

Functions and Structures Structure as function argument      int isOrigin(pointType pt){             if (pt.x == 0 && pt.y == 0)                   return 1;             else                   return 0;       }

Structures and Functions Structure as return type           pointType makePoint(int x, int y){             pointType temp;             temp.x = x;             temp.y = y;              return temp;       }  Observe there is no confusion between the two occurrences of x and y

A Screen and its Centre Point struct rectangle screen; struct point middle; struct point makePoint(int, int); screen.pt1 = makePoint(0, 0); screen.pt2 = makePoint(XMAX, YMAX); middle = makePoint((screen.pt1.x+screen.pt2.x)/2, (screen.pt1.y+screen.pt2.y)/2);

• Adding Two Points /* addPoints: add two points */ struct point addPoints(struct point p1, struct point p2) { p1.x += p2.x; p1.y += p2.y; return p1;} • Note that the local changes to p1 would not affect the point p1; pass by value