Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.

Slides:



Advertisements
Similar presentations
CS110 Programming Language I Lab 10: Arrays I Computer Science Department Spring 2014.
Advertisements

Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Basic Algorithms on Arrays. Learning Objectives Arrays are useful for storing data in a linear structure We learn how to process data stored in an array.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Simple Sorting Algorithms
Arrays Chapter 6 Chapter 6.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Selection Sort -Reading p Reading p
Lecture 12 Oct 13, 2008 Some simple recursive programs recursive problem solving, connection to induction Some examples involving recursion Announcements.
Complexity Analysis (Part III ) Analysis of Some Sorting Algorithms. Analysis of Recursive Algorithms.
Time Complexity s Sorting –Insertion sorting s Time complexity.
Data Structures Review Session 1
Jan Art of Programming Yangjun Chen Dept. Business Computing University of Winnipeg.
Lec2_Java1 4 Data Types 4 Operators 4 Control Flow Statements 4 Arrays 4 Functions -- Static Methods 4 View Classes as modules Introduction to the Java.
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
1 Sorting Algorithms (Part I) Sorting Algoritms (Part I) Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Search Algorithms Sequential Search It does not require an ordered list. Binary Search It requires an ordered.
1 Design and Analysis of Algorithms تصميم وتحليل الخوارزميات (311 عال) Chapter 1 Introduction to Algorithms.
Sort an array - the selection sort algorithm. Selection Sort Selection sort a classic computer algorithm that is used to sort an array The selection sort.
Sorting Text Read Shaffer, Chapter 7 Sorting O(N 2 ) sorting algorithms: – Insertion, Selection, Bubble O(N log N) sorting algorithms – HeapSort, MergeSort,
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
Java Generics.
Building Java Programs Chapter 13 Searching reading: 13.3.
EENG212 Algorithms and Data Structures
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
CSC 205 Java Programming II Algorithm Efficiency.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Ics202 Data Structures. class Array { protected Object[] data; protected int base; public Array (int n, int m) { data = new Object[n]; base = m; } public.
Answers to Assignment #1 (Assignment due: Wed. Feb. 05, 2003) 1. What does the "plateform independence" mean? How is it implemented in Java? 2. Summarize.
Asymptotic Notation (O, Ω, )
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Working with arrays (we will use an array of double as example)
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Sorting an array bubble and selection sorts. Sorting An arrangement or permutation of data An arrangement or permutation of data May be either: May be.
Catie Welsh April 20,  Program 4 due Wed, April 27 th by 11:59pm  Final exam, comprehensive ◦ Friday, May 6th, 12pm  No class Friday - Holiday.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
1 Insertion sort [ ] i=1 j=1 i=2 j=2 insert i=3 at j=1 [ ] i=3 j=1 insert i=4 at j=0 [
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Simple algorithms on an array - compute sum and min.
CS 162 Intro to Programming II Insertion Sort 1. Assume the initial sequence a[0] a[1] … a[k] is already sorted k = 0 when the algorithm starts Insert.
Computer Programming Lab 9. Exercise 1 Source Code package excercise1; import java.util.Scanner; public class Excercise1 { public static void main(String[]
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.
Building Java Programs Chapter 13 Sorting reading: 13.3, 13.4.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Midterm 2 Review Notes on the CS 5 midterm Take-home exam due by 5:00 pm Sunday evening (11/14) Hand in your solutions under the door of my office, Olin.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
COP 3503 FALL 2012 Shayan Javed Lecture 16
using System; namespace Demo01 { class Program
RECITATION 1 ANALYSIS OF ALGORITHMS
Function Call Trace public class Newton {
Chapter 8: Collections: Arrays
Computing Adjusted Quiz Total Score
Selection sort Given an array of length n,
محاور المحاضرة خوارزمية الفقاعات الهوائية (Bubble Sort)
Recursive GCD Demo public class Euclid {
class PrintOnetoTen { public static void main(String args[]) {
Building Java Programs
Why did the programmer quit his job?
Building Java Programs
Building Java Programs
Building Java Programs
Sorting Algorithms.
Presentation transcript:

Session 3 Algorithm

Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check an integer is an even number or not Algorithm Input a re = a %2 if(re == 0) then a is an even number else a is an odd number

Finding maximum, minimum a is an array of integers. Finding maximum, minimum of array a max = a[0], min = a[0] Compare max, min with remain a[i], 1<=i<a.length If max < a[i] then max = a[i] If min > a[i] then min = a[i] Learn Java By Example 3/25

Example a[] = {12, -3, 15, 6, 7} z[0] = 12, a[1] = -3, a[2] = 5, a[3] = 6, a[4] = 7 max = a[0] = 12 i = 1  max = 12 i = 2  max < a[2]  max = 15 i = 3  max = 15 i = 4  max = 15

Source code class Maximum{ public static void main(String args[]){ int a[] = {12, -3, 15, 6, 7}; int max = a[0]; for(int i = 1 ; i < a.length ; i++) if(max < a[i]) max = a[i]; System.out.println(max); }

Sorting array a is an array of integers. Sorting array a in ascending order i = 0: finding minimum then place it at a[0] i = 1: finding minimum from remain elements of array a then place it at a[1] i = a.length - 2: finding minimum from remain elements of array a then place it at a[a.length - 2]

Example a[] = {12, -3, 15, 6, 7} i=0, min = -3 i=1, min = 6 i=2, min = 7 i=3, min = 12  a[] = {-3, 6, 7, 12, 15}

Source code class Sorting{ public static void main(String args[]){ int a[] = {12, -3, 15, 6, 7}; for(int i = 0 ; i < a.length - 1 ; i++) for(int j = i+1 ; j < a.length ; j++) if(a[i] > a[j]){ int tam = a[i]; a[i] = a[j]; a[j] = tam; } for(int i = 0 ; i < a.length ; i++) System.out.print (a[i] + “\t”); }

Symmetric aray a is an array of integers. Check it is a symmetric array or not a[] = {12, -3, 15, 6, 7} i=0, a[0] == a[4]. If not exist, else continue i=1, a[1] == a[3]. If not exist, else continue  a[i] = a[a.length – i +1], 0<=i<a.length/2

Source code class Symmetric{ public static void main(String args[]){ int a[] = {12, -3, 15, 6, 7}; for(int i = 0 ; i < a.length/2 ; i++) if(a[i] != a[a.length-i+1]){ System.out.println(“Asymmetric array”); return; } System.out.print (“symmetric array”); }

Finding the first appearance a is an array of integers. b is an integer. Finding the first appearance of b in a a[] = {12, -3, 5, 6, 5} class FirstAppearance{ public static void main(String args[]){ int a[] = {12, -3, 5, 6, 5}; int b = 5; for(int i=0 ; i<a.length ; i++) if(b == a[i]){ System.out.println(“First appearance is” + i); return; } System.out.println(b + “ not appearance in a”); }