Arrays - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Arrays

Description:

Arrays are static' entities, in that they remain the same size once they are created ... The linear search works well for small or unsorted array. ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 13
Provided by: PROFE172
Category:
Tags: arrays | unsorted

less

Transcript and Presenter's Notes

Title: Arrays


1
Arrays
  • Arrays are data structures consisting of data
    items of the same type.
  • Arrays are static entities, in that they remain
    the same size once they are created
  • MC provides manages arrays
  • Managed arrays are actually objects of
    SystemArray class

2
Array
  • Declaring and allocating arrays
  • int c __gc
  • // declare a managed array
  • c new int __gc12
  • // allocate space for array
  • Arrays are allocated with new because arrays are
    objects and all objects must be created with new
  • Managed types are garbage collected and managed
    by the CLR part of the .NET framework that
    executes MC program
  • Managed arrays inherit from class SystemArray.

3
  • double array1 __gc new double __gc10
  • String b new String100,
  • x new String27
  • Double array new Double10
  • Note that we do not have to include keyword __gc,
    because String represents a pointer to a
    managed type

4
Using array
  • Every array in MC knows its own length.
  • c-gtLength
  • const int ARRAY_SIZE 10
  • int z __gc
  • z new int __gcARRAY_SIZE
  • for( int i 0 i ltz-gtLength i)
  • zi 2 2i
  • for( int i 0 i lt ARRAY_SIZEi)
  • ConsoleWrite(S0\t\t, zi.ToString())

5
Sorting Arrays
  • Sorting data is one of the most important
    computing applications.
  • Bubble sortuse nested loops to make several
    passes through the array. Each pass compares
    successive pairs of elements. If the pair is in
    decreasing order, the bubble sort swaps the
    values in the array
  • ArraySort(a)

6
Bubble sort
  • void bubblesort(int b __gc)
  • int pass, I, hold
  • for (pass 1 pass lt b-gtLength pass)
  • for( I 0 I ltb-gtLength 1 I)
  • if( bI gt bI1 )
  • hold bI
  • bI bI1
  • bI1 hold

7
Searching arrays
  • The process of locating a particular element
    value in an array is called searching
  • Often, programmers work with large amount of data
    stored in arrays and often need to determine
    whether an array contains a value that matches a
    certain key value.
  • Two searching techniques
  • Simple linear search
  • Binary search

8
Linear search
  • Function linear search uses a for loop to compare
    each element to the key value. If the key is
    found, the function return the subscript of the
    element if the the search key is not found, the
    function return 1
  • int linearsearch(int array __gc, int key)
  • for (int I 0 I ltarray-gtLengthI)
  • if( arrayI key) return I
  • return 1

9
Binary search
  • The linear search works well for small or
    unsorted array. However, for large arrays, linear
    searching is inefficient.
  • Since arrays can be stored as sorted.
  • High-speed binary search can be used on large
    sorted array

10
Binary search algorithm
  • The algorithm locates the middle array element
    and compares it with the search key. If they are
    equal, the search key is found, and the subscript
    is returned otherwise, the problem is reduced to
    searching half of the array. If the key is less
    than the middle element, the first half of the
    array is searched otherwise, the second half of
    the array is searched. The search continues until
    the key is found or subarray consists one element
    that is not equal to the search key( the search
    key is not found)

11
Binarysearch function
  • int BinarySearch(int array __gc, int key)
  • int low0, higharray-gtLength-1, middle
  • while( low lt high )
  • middle (lowhigh)/2
  • if( key arraymiddle ) return middle
  • else if( key lt arraymiddle) high middle-1
  • else low middle 1
  • return 1

12
Practice
  • Use a one-dimensional array to solve the
    following problem. A company pays its salespeople
    on a commission basis. The salespeople receive
    200 per week, plus 9 of their gross sales for
    that week. For example, a salesperson who grosses
    5000 in sales in a week receives 200 plus 9 of
    5000, or a total of 650. Write a program (using
    an array of counters) that determines how many of
    the salespeople earned salaries in each of the
    following ranges (assume that each salespersons
    salary is truncated to an integer amount)
  • 200 299 300 399 400 499
  • 500 599 600 699 700 799
  • 800 899 900 999 1000 and over
Write a Comment
User Comments (0)
About PowerShow.com