Fundamental of Programming...
Transcript of Fundamental of Programming...
![Page 1: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/1.jpg)
Lecturer: Mahdi Soltani
Sharif University of TechnologyDepartment of Computer Engineering
Fundamental of Programming (C)
Lecture 7
Array typical problems, Search, Sorting
![Page 2: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/2.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 2/73
Outline• Array typical problems
• Search
• Sorting
![Page 3: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/3.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 3/73
Find Maximum• Find maximum value in data array
![Page 4: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/4.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 4/73
Find Average
![Page 5: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/5.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 5/73
Number of elements greater than average
• After finding the average as shown in previous slide, use the following code
![Page 6: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/6.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 6/73
Find pair sum Find sum of every pair in data and write into pair array
data[0]=5
data[1]=7
data[2]=15
data[3]=5
…
…
data[98]=3
data[99]=12
pair[0]=12
pair[1]=20
…
pair[49]=15
}}
}
.
.
.
![Page 7: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/7.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 7/73
solution
![Page 8: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/8.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 8/73
Randomly re-shuffle numbers 30 timesdata[0]=5
data[1]=7
data[2]=15
data[3]=5
…
…
data[98]=3
data[99]=12
.
.
.
data[0]=12
data[1]=7
data[2]=5
data[3]=15
…
…
data[98]=3
data[99]=5
![Page 9: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/9.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 9/73
solution
![Page 10: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/10.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 10/73
Reverse an array
6 3 1 9 7 2
0 21 3 4 5
2 7 9 1 3 6
![Page 11: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/11.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 11/73
Reverse an Array
![Page 12: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/12.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 12/73
Print sum of top-bottom pairsA[0]=3
A[1]=6
…
A[49]=5
A[50]=3
A[98]=4
A[99]=5
+ + +….
![Page 13: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/13.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 13/73
Group avg• Suppose we have a sorted array of hundred
grades.
• We want to find the average oftop ten, second top ten students etc.
}Grade[0]
Grade[1]
….
Grade[9]
Grade[10]
…
Grade[19]
Grade[20]
…
Grade[90]
….
Grade[99]
}
}
![Page 14: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/14.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 15/73
#include <stdio.h>
#define SIZE 20
void main(void){
int number[SIZE];
double average;
int sum, large_size, small_size, i;
}
sum = large_size = small_size = 0;
for(i = 0; i < SIZE; i++){
int tmp;
scanf("%d", &tmp);
number[i] = tmp;
sum += number[i];}
average
for(i =
= (1.0 * sum) / SIZE;
0; i < SIZE; i++)
= %d\n", small_size, large_size);
برنامهایبنویسیدکهبیستعددازورودیترازدریافتکندوتعداداعدادکوچکتروبزرگ
.میانگینراچاپکند
if(number[i] >= average)
large_size++;
else
small_size++;
printf("average = %f\n", average);
printf("Small Size = %d, Larg Size
![Page 15: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/15.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 17/73
Array Elements in Functions
int number[20];
number[i] is an integer
variable
Array element can be used for
call by value input
Array element can be use for
output
![Page 16: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/16.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 18/73
Arrays in Functions
Array cannot be used as output type of
function
int [] f(int x, int y); //compile error
Arrays can be used in input list of functions
Arrays are not passed by Call By Value
Arrays are passed by Call By Reference
If we change array elements in a function
The element is changed in the caller function
![Page 17: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/17.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 19/73
Arrays in FunctionsSize of array could be different from the real oneBut dangerous things would happen
![Page 18: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/18.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 20/73
Exercise
a[0]=3
a[1]=5
c=? 8
b=
n=2
i=0 1 2
sum=0 3 8
![Page 19: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/19.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 21/73
Exercise
a[0]=3 20
a[1]=5
c=? 8
b=
n=2
i=0 1 2
sum=0 3 8
![Page 20: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/20.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 22/73
وگیردمیرا10بهطولرایهآتابعیکهیکمقداردهی9تا0اعضایآنرابااعداد
.میکند
![Page 21: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/21.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 23/73
Array Size in Functions If array is an input parameter of
a function
It cannot find out the size of the
array
Array size should be passed
from caller function to called
function
Using definitions
![Page 22: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/22.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 24/73
Array Size in Functions (cont’d) If array is declared in a function It knows the size of the array
It can find out the size of the array using sizeof
![Page 23: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/23.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 25/73
توتابعیبنویسیدکهیکآرایهرادریاف.محلبزرگترینعنصرآنرابازگرداند
![Page 24: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/24.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 26/73
Search function• Liner search
• Binary search
![Page 25: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/25.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 27/73
Unordered list – linear search
![Page 26: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/26.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 28/73
Ordered list – linear search
![Page 27: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/27.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 29/73
Binary Search
Key = 76
![Page 28: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/28.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 30/73
Binary Search
![Page 29: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/29.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 31/73
Sort function• Selection Sort
• Bubble Sort
![Page 30: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/30.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 32/73
Selection Sort
![Page 31: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/31.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 33/73
Implementation
![Page 32: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/32.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 34/73
Bubble sort • Bubble sort Several passes through the array
– Successive pairs of elements are compared • If increasing order (or identical ), no change
• If decreasing order, elements exchanged
– Repeat
34
![Page 33: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/33.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 35/73
Bubble Sort
![Page 34: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/34.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 36/73
Bubble Sort
![Page 35: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/35.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 37/73
Bubble Sort
![Page 36: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/36.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 38/73
Bubble Sort
![Page 37: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/37.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 39/73
Bubble Sort
![Page 38: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/38.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 40/73
Bubble Sort
![Page 39: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/39.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 41/73
Bubble Sort
![Page 40: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/40.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 42/73
Bubble Sort
![Page 41: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/41.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 43/73
Bubble Sort
![Page 42: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/42.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 44/73
Bubble Sort
![Page 43: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/43.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 45/73
Bubble Sort
![Page 44: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/44.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 46/73
Bubble Sort
![Page 45: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/45.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 47/73
Bubble Sort
![Page 46: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/46.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 48/73
Bubble Sort
![Page 47: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/47.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 49/73
Bubble Sort
![Page 48: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/48.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 50/73
Bubble Sort
![Page 49: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/49.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 51/73
Bubble Sort
![Page 50: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/50.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 52/73
Bubble Sort
![Page 51: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/51.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 53/73
Bubble Sort
![Page 52: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/52.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 54/73
Bubble Sort
![Page 53: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/53.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 55/73
Bubble Sort
![Page 54: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/54.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 56/73
Bubble Sort
![Page 55: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/55.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 57/73
Bubble Sort
![Page 56: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/56.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 58/73
Implementation
![Page 57: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/57.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 59/73
Merge two sorted array• Assume we have A and B arrays containing
sorted numbers
• For example– A = { 3, 5, 7, 9, 12}
– B = {4, 5, 10}
• Merge these two arrays as a single sorted array C, for example– C = {3, 4, 5, 5, 7, 9, 10, 12}
![Page 58: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/58.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 60/73
Solution
![Page 59: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/59.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 61/73
Intersection Set • Suppose we have two sets (groups) represented by A
and B
• E.g., A is the set of students taking Math,
B is the set of students taking Science.
• Find set C, the intersection of A and B, i.e., students taking both Math and Science
For each element ID in A
Search that ID in B
if found, put ID into C 3 6 9 17 2
458
![Page 60: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/60.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 62/73
Solution
6 3 1 9 7 2
4 2 5 6 1 8
6 1 2
![Page 61: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/61.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 63/73
Multidimensional Arrays in Functions
Can be used as input of functionsAll dimensions except the first one must be given
void func(int a[10][20][5]);
Input is a 10x20x5 integer matrix
void func(int a[][20][30], int size); void func(int
size1, int size2, int
a[size1][size2]);
Input is a matrix of integers that both rows and columns are
variable
![Page 62: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/62.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 64/73
Multidimensional Arrays in Functions
The first subscript therefore only indicates the amount of storage that is needed when the array is declared
Others are required because the computer needs to know how far along to increment the pointer for each "row"
![Page 63: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/63.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 65/73
#define
}
SIZE 5
void swap(int a[SIZE][SIZE], int i, int j){
int tmp;
tmp = a[i][j];
a[i][j]
a[j][i]
= a[j][i];
= tmp;
}
void transpose(int a[][SIZE]){
for(i =
int i, j;
0; i < SIZE; i++)
for(j = i; j < SIZE; j++)
swap(a, i, j);
ماتريس ترانهاده محاسبه
![Page 64: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/64.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 67/73
2-Dim Arrays as Arguments to Functions
void print_m(int m[][4], int r, int c)
{
int i,j;
for (i=0; i < r; i++) {
for (j=0; j < c; j++)
printf("%.5d ",m[i][j]);
printf("\n");
}
printf("\n");
return;
}
int i, j, matrix[3][4];
for (i=0; i<3; i++)
for (j=0; j<4; j++)
matrix[i][j] = i;
print_m(matrix, 3, 4);
void print_m(int m[3][4], int r, int c)
![Page 65: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/65.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 68/73
Matrix sum• Compute the addition of two matrices
3 0 33
0 6 6 3
2 0 4 4
0 1 2 3
0
1
2
0 1 20
-1 2 4 3
0 -1 3 1
0
1
2
0 1 2 3
+
3 -1 13
1 4 2 0
2 1 1 3
0
1
2
0 1 2 3
=
![Page 66: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/66.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 69/73
solution
int matrix1[3][4],
matrix2[3][4],
sum[3][4];
// initialize matrix1 and matrix2
for (i=0; i<3; i++)
for (j=0; j<4; j++)
sum[i][j]=
matrix1[i][j]+matrix2[i][j];
![Page 67: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/67.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 70/73
Exchange Two Rows
4 6 2
0 5 3
0 8 1
2 1 4
4 6 2
2 1 4
0 8 1
0 5 3
![Page 68: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/68.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 71/73
Matrix multiplicationdouble a[3][2], b[2][4], c[3][4];
• Find c = a * b;
x =
3*2 + 4*4=22 3*3 + 4*5=29
2 3 7 1
4 5 6 8
3 4
5 2
1 6
22 29 45 35
18 40 47 21
26 33 43 49
5*2 + 2*4=18
3*7 + 4*6=45 3*1 + 4*8=35
5*3 + 2*5=40 5*7 + 2*6=47 5*1 + 2*8=21
1*2 + 6*4=26 1*3 + 6*5=33 1*7 + 6*6=43 1*1 + 6*8=49
![Page 69: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/69.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 72/73
Matrix Multiplication
x =2 3 7 1
4 5 6 8
3 4
5 2
1 6
22 29 45 35
18 40 47 21
26 33 43 49
i=0
i
jj
3 42
4x
j=0
k
k =
c[i][j] =
a[i][k=0]*b[k=0][j] +
a[i][k=1]*b[k=1][j]
0 1 2 3
i
0
1
2
0 1 2 3
0
1
2
![Page 70: Fundamental of Programming (C)ce.sharif.edu/courses/97-98/1/ce153-7/resources/root/Slides/Lecture… · Fundamental of Programming (C) Lecture 7 Array typical problems, Search, Sorting.](https://reader034.fdocuments.in/reader034/viewer/2022042515/5f852e9ae41e82179415f98c/html5/thumbnails/70.jpg)
Array typical problems, Search, Sorting – Lecture 8
Sharif University of TechnologyDepartment of Computer Engineering 73/73
Matrix Multiplication cont’d#define N 3
#define M 2
#define L 4
void matrix_mul(a[N][M], int b[M][L], int c[N][L])
{
int i, j, k;
for(i=0; i < N; i++) {
for(j=0; j < L; j++) {
c[i][j] = 0;
for(k=0; k < M; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return;
}