Download CSE1320-001-Fall2013 INTERMEDIATE PROGRAMMING

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSE 1320
Arrays, Search and Sort
Dr. Sajib Datta
CSE@UTA
Multidimensional Array
 We can also have arrays of arrays, also known as
multidimensional arrays.
 E.g., A two-dimensional array is an array of several one-
dimensional arrays.
 The basic form for declaring a 2D array is
 type array_name[rows][columns];
 The mathematical equivalent of a two-dimensional array is a
matrix.
2-D Array
 A 2D array can be viewed like a table.
10
5
13
2
7
19
 To create a 2D array for this table of values, we could use
 int some_data[2][3] = { {10, 5, 13}, {2, 7, 19} };
 Or
 int some_data[][3] = { {10, 5, 13}, {2, 7, 19} };
2-D Array
 As with one-dimensional arrays, 2D arrays indices begin at zero.
The difference is now we have to keep track of indices in two
dimensions. So, if we create the following array:
 int Sales[2][3] = { {1, 2, 3}, {4, 5, 6} };
 The individual elements are referenced with the following
combinations of array name and indices:
2-D Array
Print out a specific row or column in an array
#include <stdio.h>
int main(void)
{
int row, column, Sales[2][3] = { {1, 2, 3}, {4, 5, 6} };
printf("The values on row 0 are : \n");
for (column = 0; column < 3; column++)
printf("%d ", Sales[0][column] );
printf("\n");
printf("The values on column 0 are: \n");
for (row = 0; row < 2; row++)
printf("%d\n", Sales[row][0]);
return 0;
}
Copying an array
 In case of normal variables: to assign value of ‘a’ to ‘b’;
 int a,b;
 a=10;
 b = a;
 Assume we want to copy the values of array ‘a’ to array ‘b’
 int a[10], b[10];
 Initialize a;
 b = a;//wrong
Copying an array
 Have to iterate over ‘a’ and assign individual array elements
into ‘b’
for(i=0;i<10;i++)
b[i] = a[i];
Linear search
 Given an array on integers and another integer ‘x’, find if ‘x’
appears in the array. Print ‘Not found’, if ‘x’ does not appear,
otherwise print ‘Found’.
 Strategy:
 Simply iterate over the array if look for a match
Linear search
int array[100];
int i,x, found=0;
….
….
for(i=0; i<100;i++)
{
if( array[i] == x)
{
found =1;
printf(“Found %d”,x);
break;
}
}
if(!found)
{
printf(“%d not found”, x);
}
Linear search
 The worst case – the loop is executed n times
 On the average – the loop is executed n/2 times
 What if the array has a million integers (about 220)
Binary Search
 Can make it faster.
 However, unlike linear search, binary search has a
precondition
 The sequence must be sorted
Binary Search
 Strategy:
 Compare ‘x’ with the middle element.
 If equal, we found it
 Else If ‘x’ < middle element, we can discard the half of the elements; all
elements that are greater than the middle element and middle element
itself
 Now search in the remaining half of the array
 Similarly, if ‘x’ > middle element, we can discard the half of the elements;
all elements that are smaller than the middle element and middle element
itself
Binary Search
#include <stdio.h>
#define ARRSIZE 7
int main(void)
{
int intarr[ARRSIZE], target, i, left, right, mid;
printf("Please input %d integers which are sorted
in the ascending order and a target value.",
ARRSIZE);
scanf("%d", &target);
for(i = 0; i<ARRSIZE; i++)
{
scanf("%d", &intarr[i]);
}
left = 0;
right = ARRSIZE-1;
while(left <= right)
{
mid = (left+right)/2;
if(intarr[mid] == target)
{
printf("The index of the target in the array
is %d.\n", mid);
break;
}
else if (target > intarr[mid])
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
if(left > right)
printf("The target is not in the array.\n");
return 0;
}
Binary Search
 How many comparisons on average?
 Each time ‘x’ is not found, we are taking the half of the
remaining array
 If the array has size ‘n’, how many times we can slice it into
half?
Assume you can compare at most ‘m’
times
 After 1st half: array size n/2
 After 2nd half: array size n/4
 After 2rd half: array size n/8
 ….
 ….
 After m-th half: array size n/(2m)
 Since, the minimum array size is 1 [there has to be at least 1
element]
 n/(2m) = 1 => m = log2(n)
Performance
 So, in binary search, there are log2(n) comparisons.
 If you are given a sorted array of a million integers (220),
 Linear search can make a million comparison operations
 Binary search will make around 20 comparisons
 You will learn more on performance in algorithm classes. This is
very important aspect of programming.
Practice problem
 You are given an array containing 9 integers, ranging from 1
to 10. Each number between 1 to 10 appears exactly once in
the array, but unfortunately one of them is missing. Can you
find the missing number?
CSE1320
Arrays and Strings
Dr. Sajib Datta
CSE@UTA
Sort
 Ordering elements in some way
 For numeric data, ascending order is the most common
 Lots of techniques for sorting
 These techniques vary in performance both
with runtime and usage of extra memory
Bubble sort
 Given a sequence of numbers, it compares
adjacent numbers and swap them if necessary
 Repeats the above procedure until all numbers are in right
place
An example
4
10
9
Pick 4, compare with 10
6
-1
An example
4
10
9
Pick 10, compare with 9
Need to swap
6
-1
An example
4
9
10
6
-1
An example
4
9
10
Pick 10, compare with 6
Need to swap
6
-1
An example
4
9
6
10
-1
An example
4
9
6
Pick 10, compare with -1
10
-1
An example
4
9
6
-1
10
Notice, that 10 is at the right position
Now, repeat from the beginning
An example
4
9
6
-1
10
Compare 1st element with 2nd element
An example
4
9
6
-1
10
Compare 2nd element with 3rd element
Swap needed
An example
4
6
9
-1
10
An example
4
6
9
-1
10
Compare 3rd element with 4th element
Swap needed
An example
4
6
-1
9
10
An example
4
6
-1
9
10
Now, do we need the compare 4th with the 5th?
We already know 5th element is the largest.
This implies, what we have as the 4th element, it must be the
second largest, and is already in the correct place.
An example
4
6
-1
9
10
Now, repeat from the beginning again. But remember, we only
need to go up to 3rd this time.
What is happening inside…
 1st iteration places the largest number in the correct place
 2nd iteration places the second largest number in the correct




place
…..
….
….
i-th iteration places the i-th largest number in the correct
place
Bubble sort
#include<stdio.h>
#define ARR_SIZE 8
int main(void)
{
int intarr[ARR_SIZE] = {23, 4, 12, 8, 22, 1, 54, 9}, i, j, tmp;
for(i = 0; i<ARR_SIZE-1; i++)
{
for(j = 0; j<ARR_SIZE-i-1; j++)
{
if(intarr[j]>intarr[j+1])
{
tmp = intarr[j];
intarr[j] = intarr[j+1];
intarr[j+1] = tmp;
}
}
}
printf("The array sorted by Bubble Sort: ");
for(i = 0; i< ARR_SIZE; i++)
printf("%d ", intarr[i]);
return 0;
}
Related documents