ARRAYS Lecture 14 11.2.2002.. void reverse (int x[], int size) { int i; for (i=0; i< (size/2); i++) temp = x[size-i-1] ; x[size-1-1] = x[i] ; x[i] = temp;

Slides:



Advertisements
Similar presentations
Topic 14 Searching and Simple Sorts "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The.
Advertisements

CSE Lecture 3 – Algorithms I
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Q-1 University of Washington Computer Programming I Lecture 16: Sorting © 2000 UW CSE.
Lecture 22: Arrays (cont). 2 Lecture Contents: t Searching in array: –linear search –binary search t Multidimensional arrays t Demo programs t Exercises.
HST 952 Computing for Biomedical Scientists Lecture 9.
System Programming in C Lecture 4. Lecture Summary Operations with Arrays: –Searching the array –Sorting the array.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Algorithm Efficiency and Sorting
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
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
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Functions A Problem Suppose we are writing a program that displays messages on the screen. We want to display rows of ======================== =======
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
1 1-d Arrays. 2 Array Many applications require multiple data items that have common characteristics  In mathematics, we often express such groups of.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Searching an Array: Linear and Binary Search Spring 2013Programming and Data Structure1.
Computer Science Searching & Sorting.
1 Linear and Binary Search Instructor: Mainak Chaudhuri
Searching CS 105 See Section 14.6 of Horstmann text.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
C# PROGRAMMING Searching & Sorting. Objective/Essential Standard Essential Standard 3.00 Apply Advanced Properties of Arrays Indicator 3.03 Apply procedures.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
CSC 211 Data Structures Lecture 13
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Sorting. 2 contents 3 kinds of sorting methods – Selection, exchange, and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements – Simple.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Chapter 3 Searching and Selection Algorithms. 2 Chapter Outline Sequential search Binary search List element selection.
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.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
M180: Data Structures & Algorithms in Java Sorting Algorithms Arab Open University 1.
Q-1 University of Washington Computer Programming I Lecture 16: Sorting © 2000 UW CSE.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
Data Structures Arrays and Lists Part 2 More List Operations.
Searching CS 110: Data Structures and Algorithms First Semester,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Sorting and Searching Bubble Sort Linear Search Binary Search.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Recursion Riley Chapter 14 Sections 14.1 – 14.5 (14.6 optional)
Programming and Data Structures
1-d Arrays.
COP 3503 FALL 2012 Shayan Javed Lecture 15
Lecture 14 Searching and Sorting Richard Gesick.
Sorting and Searching Sudeshna Sarkar 7th Feb 2017.
ARRAYS Lecture
CSC215 Lecture Algorithms.
Lecture 11 Searching and Sorting Richard Gesick.
ARRAYS ..
Presentation transcript:

ARRAYS Lecture

void reverse (int x[], int size) { int i; for (i=0; i< (size/2); i++) temp = x[size-i-1] ; x[size-1-1] = x[i] ; x[i] = temp; } int findmax (int x[], int size) { int i, max; max = x[0]; for (i=1; i< size; i++) if (x[i] > max) max = x[i] ; return max; }

int findmax (int x[], int size) { if (size ==1) return x[0] ; maxl = findmax (x, size-1) ; if (x[0] > max) max = x[0]; return max; }

Strings Strings are 1-dimensional arrays of type char. By convention, a string in C is terminated by the end-of-string sentinel \0, or null character. String constant : “abc” is a character array of size 4, with the last element being the null chaaracter \0. char s[] = “abc” ; abc\0

Searching an Array: Linear and Binary Search

Searching Check if a given element (key) occurs in the array. If the array is unsorted : start at the beginning of the array inspect every element to see if it matches the key

Linear Search /* If key appears in a[0..size-1], return its location, pos, s.t. a[pos] == key. If key is not found, return -1 */ int search (int a[], int size, int key){ int pos = 0; while (pos < size && a[pos] != key) pos++; if (pos<n) return pos; return -1; }

Linear Search int x= {12,-3, 78,67,6,50,19,10} ; Trace the following calls : search (x, 8,6) ; search (x,8,5) ;

Searching a sorted array Binary search works if the array is sorted Look for the target in the middle If you don’t find it, you can ignore half of the array, and repeat the process with the other half.

Binary Search Strategy What we want : Find split betwen values larger and smaller than x : <=key>key <=key?>key x: 0 n LR 0nLR Situation while searching : Step : Look at [(L+R)/2]. Move L or R to the middle depending on test.

Binary Search /* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int binsearch (int x[], int size, int key){ int L, R, mid; ______________________; while (_________________){ } ____________________ ; }

/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int binsearch (int x[], int size, int key){ int L, R, mid; ______________________; while (_________________){ mid = (L+R)/2; if (x[mid] <= key) L = mid; else R = mid; } ____________________ ; } Binary Search mid = (L+R)/2; if (x[mid] <= key) L = mid; else R = mid;

/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int binsearch (int x[], int size, int key){ int L, R, mid; ______________________; while (_________________){ mid = (L+R)/2; if (x[mid] <= key) L = mid; else R = mid; } ____________________ ; } Binary Search: loop termination L+1 != R

/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int binsearch (int x[], int size, int key){ int L, R, mid; ______________________; while ( L+1 != R){ mid = (L+R)/2; if (x[mid] <= key) L = mid; else R = mid; } Binary Search: Return result if (L>=0 && x[L]==key) return L; else return -1;

/* If key appears in x[0..size-1], return its location, pos s.t. x[pos]==key. If not found, return -1 */ int binsearch (int x[], int size, int key){ int L, R, mid; ______________________; while ( L+1 != R){ mid = (L+R)/2; if (x[mid] <= key) L = mid; else R = mid; } if (L>=0 && x[L]==key) return L; else return -1; } Binary Search: Initialization L=-1; R=size;

Binary Search Examples Trace : binsearch (x, 9, 3); binsearch (x, 9, 145); binsearch (x, 9, 45);

Is it worth the trouble ? Suppose you had 1000 elements Ordinary search (if key is a member of x) would require 500 comparisons on average. Binary search after 1st compare, left with 500 elements after 2nd compare, left with 250 elements After at most 10 steps, you are done. What if you had 1 million elements ?

Sorting Given an array x[0], x[1],..., x[size-1] reorder entries so that x[0]<=x[1]<=... <=x[size-1]

Sorting Problem What we want : Data sorted in order sorted : x[0]<=x[1]<=... <=x[size-1] x: 0 size Initial conditions : unsorted x: 0 size

Selection Sort General situation : remainder, unsortedsmallest elements, sorted 0size k Step : Find smallest element, mval, in x[k..size-1] Swap smallest element with x[k], then increase k. x: smallest elements, sorted 0sizek x: mval

Subproblem : : /* Yield location of smallest element int[x] in x[0.. size-1];*/ int min_loc (int x[], int, int size) int j, pos; /* x[pos] is the smallest element found so far */ pos = k; for (j=k+1; j<size; j++) if (x[i] < x[pos]) pos = j; return pos; }

Selection Sort /* Sort x[0..size-1] in non-decreasing order */ int selsort (int x[], int size){ int k, m; for (k=0; k<size-1; k++){ m = min_loc(x, k, size); temp = a[k]; a[k] = a[m]; a[m] = temp; }

Example x: x: x: x: x: x: x:

Analysis How many steps are needed to sort n things ? Total number of steps proportional to n 2

Insertion Sort #define MAXN 100 void InsertSort (int list[MAXN], int size) ; main (){ int index, size; int numbers[MAXN]; /* Get Input */ size = readarray (numbers) ; printarray (numbers, size) ; InsertSort (numbers, size) ; printarray (numbers, size) ; }

void InsertSort (int list[], int size){ for (i=1; i<size; i++) item = list[i] ; for (j=i-1; (j>=0)&& (list[j] > i); j--) list[j+1] = list[j]; list[j+1] = item ; }

Common pitfalls with arrays in C Exceeding the array bounds int array[10]; for (i=0; i<=10; i++) array[i] = 0; C does not support array declaratiions with variable expressions. void fun (int array, int size){ int temp[size] ;... }