Академический Документы
Профессиональный Документы
Культура Документы
General definition
The basic objective of sorting is always the same: taking a
list of unorganized items, and arranging them to a certain
order.
6
Selection sort
Given an array of length n,
Search elements 0 through n-1 and select the smallest
Swap it with the element in location 0
Search elements 1 through n-1 and select the smallest
Swap it with the element in location 1
Search elements 2 through n-1 and select the smallest
Swap it with the element in location 2
Search elements 3 through n-1 and select the smallest
Swap it with the element in location 3
Continue in this fashion until there’s nothing left to
search
7
Selection Sort Algorithm
8 4 6 9 2 3 1
Alg.: SELECTION-SORT(A)
n ← length[A]
for j ← 1 to n - 1
do smallest ← j
for i ← j + 1 to n
do if A[i] < A[smallest]
then smallest ← i
exchange A[j] ↔ A[smallest]
8
Selection Sort
Selection_sort_algorithm
set current_smallest_position to 1
DOWHILE current_smallest_position ≤ (number_of_elements - 1)
set I to current_smallest_position
smallest_element = Array(I)
set J = I + 1
DOWHILE J ≤ number_of_elements
IF Array(J) < smallest_element THEN
I = J
smallest_element = Array(J)
ENDIF
J = J + 1
ENDDO
Array(I) = Array(current_smallest_position)
Array(current_smallest_position) = smallest_element
Add 1 to current_smallest_position
ENDDO
END
#include<stdio.h> for(i=0; i<SIZE; i++)
#include<conio.h> {
#define SIZE 10 min=i;
for(j=i+1; j<SIZE; j++)
if(arr[j]<arr[min])
int main()
min=j;
{
temp=arr[i];
int i,j,min,temp;
arr[i]=arr[min];
int arr[SIZE];
arr[min]=temp;
for(i=0; i<SIZE; i++) }
{ printf("After selection sort the elements:\n");
printf("Enter element : "); for(i=0; i<SIZE; i++)
scanf("%d",&arr[i]); printf("%d\t",arr[i]);
} getch();
return 0;
}
Output
Enter element : 21
Enter element : 3
Enter element : 45
Enter element : 87
Enter element : 72
Enter element : 14
Enter element : 54
Enter element : 75
Enter element : 44
Enter element : 5
23 78 45 8 32 56 Original List
8 78 45 23 32 56 After pass 1
8 23 45 78 32 56 After pass 2
After pass 3
8 23 32 78 45 56
8 23 32 45 78 56 After pass 4
After pass 5
8 23 32 45 56 78
Insertion Sort
Idea: like sorting a hand of playing cards
Start with an empty left hand and the cards facing down
on the table.
Remove one card at a time from the table, and insert it
into the correct position in the left hand
compare it with each of the cards already in the hand, from
right to left
The cards held in the left hand are sorted
these cards were originally the top cards of the pile on the
table
13
To insert 12, we need to
make room for it by moving
first 36 and then 24.
6 10 2 4 3 6
12
14
6 10 2 4 36
12
15
6 10 24 3
6
12
16
Insertion Sort
input array
5 2 4 6 1
3
at each iteration, the array is divided in two sub-arrays:
left sub-array right sub-array
sorted unsorted
17
Insertion Sort
18
Insertion-sort
Alg.: INSERTION-SORT(A) 1 2 3 4 5 6 7 8
for j ← 2 to n a 1 a2 a3 a4 a5 a6 a7 a8
do key ← A[ j ]
key
Insert A[ j ] into the sorted sequence A[1 . . j -1]
i←j-1
while i > 0 and A[i] > key
do A[i + 1] ← A[i]
i←i–1
A[i + 1] ← key
Insertion sort – sorts the elements in place
19
Insertion Sort
Insertion_sort_algorithm
set I to 1
DOWHILE I ≤ (number_of_elements - 1)
IF Array(I) > Array(I + 1) THEN
temp = Array(I)
J = I
DOWHILE (J ≥ 1 AND Array(J) > temp)
Array(J + 1) = Array(J)
J = J - 1
ENDDO
Array(J + 1) = temp
ENDIF
I = I + 1
ENDDO
END
#include<stdio.h> for (i = 1; i < num; i++) {
temp = arr[i];
int main() { j = i - 1;
int i, j, num, temp, arr[20]; while ((temp < arr[j]) && (j >= 0)) {
printf("Enter total elements: "); arr[j + 1] = arr[j];
scanf("%d", &num); j = j - 1;
}
printf("Enter %d elements: ",
num); arr[j + 1] = temp;
}
for (i = 0; i < num; i++) {
printf("After Sorting: ");
scanf("%d", &arr[i]);
for (i = 0; i < num; i++) {
}
printf("%d", arr[i]);
}
return 0;
}
Output
Enter total elements: 5
Enter 5 elements: 9 4 1 0 2
After sorting: 0 1 2 4 9
Bubble sort
Compare each element (except the last one) with its
neighbor to the right
If they are out of order, swap them
This puts the largest element at the very end
The last element is now in the correct and final place
Compare each element (except the last two) with its
neighbor to the right
If they are out of order, swap them
This puts the second largest element next to last
The last two elements are now in their correct and final places
Compare each element (except the last three) with its
neighbor to the right
Continue as above until you have no unsorted elements on the left
22
7 2 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 7 5 4 8 2 5 4 7 8 2 4 5 7 8
2 7 8 5 4 2 5 7 4 8 2 4 5 7 8 (done)
2 7 5 8 4 2 5 4 7 8
2 7 5 4 8
23
Contoh
N 8 did_swap true
to_do 7
index
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap false
to_do 7
index 1
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap false
to_do 7
index 1
Swap
98 23 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 1
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 2
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 2
Swap
23 98 45 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 2
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 3
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 3
Swap
23 45 98 14 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 3
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 4
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 98 6 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 4
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 5
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 98 67 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 5
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 6
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 98 33 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 6
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 7
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 98 42
1 2 3 4 5 6 7 8
An Animated Example
N 8 did_swap true
to_do 7
index 7
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
After First Pass of Outer Loop
N 8 did_swap true
to_do 7
index 8 Finished first “Bubble Up”
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 1
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 1
No Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 2
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap false
to_do 6
index 2
Swap
23 45 14 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 2
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
Swap
23 14 45 6 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 3
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 4
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 4
No Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
Swap
23 14 6 45 67 33 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 5
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
Swap
23 14 6 45 33 67 42 98
1 2 3 4 5 6 7 8
The Second “Bubble Up”
N 8 did_swap true
to_do 6
index 6
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
After Second Pass of Outer
Loop
N 8 did_swap true
to_do 6
index 7 Finished second “Bubble Up”
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap false
to_do 5
index 1
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap false
to_do 5
index 1
Swap
23 14 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 1
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
Swap
14 23 6 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 2
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 3
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 3
No Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
Swap
14 6 23 45 33 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 4
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
Swap
14 6 23 33 45 42 67 98
1 2 3 4 5 6 7 8
The Third “Bubble Up”
N 8 did_swap true
to_do 5
index 5
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Third Pass of Outer Loop
N 8 did_swap true
to_do 5
index 6 Finished third “Bubble Up”
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap false
to_do 4
index 1
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap false
to_do 4
index 1
Swap
14 6 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 1
Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 4
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fourth “Bubble Up”
N 8 did_swap true
to_do 4
index 4
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fourth Pass of Outer Loop
N 8 did_swap true
to_do 4
index 5 Finished fourth “Bubble Up”
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 1
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 1
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 2
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 2
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 3
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
The Fifth “Bubble Up”
N 8 did_swap false
to_do 3
index 3
No Swap
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
After Fifth Pass of Outer Loop
N 8 did_swap false
to_do 3
index 4 Finished fifth “Bubble Up”
6 14 23 33 42 45 67 98
1 2 3 4 5 6 7 8
Finished “Early”
N 8 did_swap false
to_do 3
We didn’t do any swapping,
index 4 so all of the other elements
must be correctly placed.
We can “skip” the last two
passes of the outer loop.
6 14 23 33 42 45 67 98
Bubble Sort: Algorithm
for pass = 1 .. n-1
exchange = false
for position = 1 .. n-pass
if element at position < element at position +1
exchange elements
exchange = true
end if
next position
if exchange = false BREAK
next pass
Algorithm Bubble sort
Input: unsorted sequence a1, a2, …, an
Output: sorted sequence a1, a2, …, an
for (outer = a.length - 1; outer > 0; outer--) {
// counting down
for (inner = 0; inner < outer; inner++) {
// bubbling up
if (a[inner] > a[inner + 1]) { // if out of order...
int temp = a[inner]; // ...then swap
a[inner] = a[inner + 1];
a[inner + 1] = temp;
}
}
}
98
Bubble Sort Algorithm
Alg.: BUBBLESORT(A)
for i 1 to length[A]
do for j length[A] downto i + 1
do ifi A[j] < A[j -1]
then exchange A[j] A[j-1]
8 4 6 9 2 3 1
i = 1 j
99
Bubble Sort
Bubble_sort_algorithm
set I to number_of_elements
set elements_switched to true
DOWHILE (elements_switched AND I ≥ 2)
set J to 1
set elements_switched to false
DOWHILE J ≤ I - 1
IF Array(J) > Array(J + 1) THEN
temp = Array(J)
Array(J) = Array(J + 1)
Array(J + 1) = temp
elements_switched = true
ENDIF
J = J + 1
ENDDO
I = I - 1
ENDDO
END
Bubble sorting steps
108
LINEAR SEARCH
#include<stdio.h> printf("Enter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
int main(){
if(a[i]==m){
int a[10],i,n,m,c=0; c=1;
printf("Enter the size of an array: "); break;
scanf("%d",&n); }
printf("Enter the elements of the array: "); }
if(c==0)
for(i=0;i<=n-1;i++){
printf("The number is not in the list");
scanf("%d",&a[i]); else
} printf("The number is found");
return 0;
}
Sample output:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found
Binary Search
The binary search is much more efficient than the
linear search.
It requires the list to be in order.
The algorithm starts searching with the middle
element.
If the item is less than the middle element, it
starts over searching the first half of the list.
If the item is greater than the middle element,
the search starts over starting with the middle
element in the second half of the list.
It then continues halving the list until the item is
found.
110
Binary Search
The binary search first compares the key
with the element in the middle of the
array. Consider the following three cases:
If the key is less than the middle
element, you only need to search the key
in the first half of the array.
If the key is equal to the middle
element, the search ends with a match.
If the key is greater than the middle
element, you only need to search the key
in the second half of the array.
Binary Search, cont.
key = 11
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
list 2 4 7 10 11 45 50 59 60 66 69 70 79
key = 11 mid
Example 5.11
Testing Binary Search
Objective: Implement and test the binary
search method. The program first
creates an array of 10 elements of
int type. It displays this array and
then prompts the user to enter a key
for testing binary search.
BinarySearch Run
Example 5.12
Using Arrays in Sorting
Objective: Use the selectionSort method to
write a program that will sort a list of double
floating-point numbers.
2, 9, 5, 4, 8, 1, 6
SelectionSort Run
Binary Search
Adding
Adding Deleting
Deleting
C B A
rear
rear front
front
123
Queue
Queue
Operating on both ends
Operations: EnQueue(in), DeQueue(out)
enqueu
enqueu
ee dequeue
dequeue
C B A
rear
rear front
front
124
Queue
Queue is FIFO ( First-In First-Out)
125
Queue
Queue: First In First Out (FIFO)
Input D C B A Output
Toll Station
Car comes, pays, leaves
Check-out in Big Y market
Customer comes, checks out and leaves
More examples: Printer, Office Hours, …
126
More Examples of Queue
In our daily life
Airport Security Check
Cinema Ticket Office
Bank, ATM
Anything else ?
127
Queue
Add_item_to_tail_of_queue Remove_item_to_head_of_queue
IF (queue_tail = queue_head AND IF queue_counter = 0 THEN
queue_counter > 0) THEN Print error message (‘queue is
Print error message (‘queue empty’)
overflow’) ELSE
ELSE required value = Queue (queue_head)
Queue (queue_tail) = new item queue_head = queue_head + 1
queue_tail = queue_tail + 1 IF queue_head > max_size THEN
IF queue_tail > max_size THEN queue_head = 1
queue_tail = 1 ENDIF
ENDIF queue_counter = queue_counter - 1
queue_counter = queue_counter + 1 ENDIF
ENDIF END
END
Array Implementation of
Queue
n-1 3 2 1 0
DCBA
After A leaves,
n-1 3 2 1 0
DCB
129
Stacks
Stack
An abstract data type in which accesses are made at only
one end
LIFO, which stands for Last In First Out
9 3 7 X
The composition of a Linked List
Each node in a linked list contains one or more
members that represent data.
In addition to the data, each node contains a
pointer, which can point to another node.
137
The composition of a Linked List
A linked list is called "linked" because each node in
the series has a pointer that points to the next node
in the list.
138
Linked Implementation
Linked implementation
An implementation based on the concept of a node
Node
A holder for two pieces of information
the item that the user wants in the list (item)
a pointer to the next node in the list (next)
Linked Implementation