/
ACM Programming Competition ACM Programming Competition

ACM Programming Competition - PowerPoint Presentation

alexa-scheidler
alexa-scheidler . @alexa-scheidler
Follow
376 views
Uploaded On 2018-02-26

ACM Programming Competition - PPT Presentation

Sorting Algorithms Sorting Algorithms ACM Programming Competition Dr Thang Dinh tndinhvcuedu Dr Alberto Cano acanovcuedu Web httpwwwpeoplevcuedutndinhacm ACM Programming Competition ID: 636636

map acm competition sort acm map sort competition programming sorting entry algorithms quicksort put min swap key entries mid

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "ACM Programming Competition" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

ACM Programming Competition

Sorting Algorithms

Sorting Algorithms

ACM Programming Competition

Dr. Thang

Dinh

tndinh@vcu.edu

Dr. Alberto Cano

acano@vcu.edu

Web:

http://www.people.vcu.edu/~tndinh/acm/Slide2

ACM Programming Competition

Sorting Algorithms

Terminology

Classification

O(n

2

)

O(n log n)

Built-in sorting methods

Key-value sorting

ACM sorting problemsSlide3

ACM Programming Competition

Terminology

Sorting: order an array of keys whose elements are comparable

Internal (in-place) vs external (require extra memory)

Stable: maintain the relative order for equal keys

Recursion: divide and conquerSlide4

ACM Programming Competition

ClassificationSlide5

ACM Programming Competition

ClassificationSlide6

ACM Programming Competition

O(n

2

) algorithms

Insertion sort

for

i

← 1

to

length(A)

j ←

i

while

j > 0 and A[j-1] > A[j]

swap

A[j] and A[j-1]

j ← j – 1

end while

end for

Slide7

ACM Programming Competition

O(n

2

) algorithms

Selection sort

for

i

← 1

to

length(A)-1

min ←

i

for

j

← i+1

to

length(A)

if

A[j] < A[min]

min

j

end if

j ← j – 1

end for

if

min != j

swap

A[j] and A[min]

end if

end for

Slide8

ACM Programming Competition

O(n

2

) algorithms

Bubble sort

repeat

swapped

false

for

i

← length(A)-1

to

1

if

A[

i

]

>

A[i+1]

swap

A[

i

] and [i+1]

swapped

true

end if

end for

until not swappedSlide9

ACM Programming Competition

O(n log n) algorithms

Quicksort

quicksort

(A, lo, hi)

if

lo < hi

p

partition(A, lo, hi)

quicksort(A, lo, p - 1)

quicksort(A, p + 1, hi)

partition

(A, lo, hi)

pivot

A[hi]

i

lo

for

j

lo

to

hi - 1

if

A[j] <= pivot

swap

A[

i

] and A[j]

i

i + 1

swap

A[i

] and A[hi]

return iSlide10

ACM Programming Competition

O(n log n) algorithms

Merge sort

mergesort

(A, lo, hi)

if

lo+1 < hi

mid = (lo + hi) / 2

fork

mergesort

(A, lo, mid)

mergesort

(A, mid, hi)

join

merge(A, lo, mid, hi)Slide11

ACM Programming Competition

ComparisonSlide12

ACM Programming Competition

Built-in sorting methods

Arrays.sort

(T[] A) Quicksort

Arrays.parallelSort

(T[] A) Parallel Quicksort

Collections.sort

(List<T> list) Merge sort

Collections.sort

(

personList

,

new

Comparator

<Person>(){

public

int

compare

(Person p1, Person p2){

return

p1.firstName.

compareTo

(p2.firstName);

}

});Slide13

ACM Programming Competition

Key-value sorting

Map<K,V>

TreeMap

<K,V> implements

SortedMap

<K,V>

Map<

Integer,String

>

map =

new

TreeMap

<

Integer,String

>

();

map.put

(8, "name8");

map.put

(35, "name35");

map.put

(44, "name44");

map.put

(7, "name7");

map.put

(6, "name6");

for

(

Map.Entry

<Integer, String>

mapData

:

map.entrySet

()) {

System.out.println

("Key : " +

mapData.getKey

() +

"Value : “ +

mapData.getValue());

}Slide14

ACM Programming Competition

Key-value sorting

public static <K, V extends Comparable<? super V>> Map<K, V>

sortByValue

(final Map<K, V>

mapToSort

) {

List<

Map.Entry

<K, V>> entries =

new

ArrayList

<

Map.Entry

<K, V>>

(

mapToSort.size

());

entries.addAll

(

mapToSort.entrySet

());

Collections.sort

(entries, new Comparator<

Map.Entry

<K, V>>() {

@Override

public

int

compare(final

Map.Entry

<K, V> entry1, final

Map.Entry<K, V> entry2){

return entry1.getValue().compareTo(entry2.getValue());

}

});

Map<K, V> sortedMap

= new LinkedHashMap

<K, V>();

for (

Map.Entry<K, V> entry : entries) {

sortedMap.put

(entry.getKey(),

entry.getValue());

}

return

sortedMap

;}Slide15

ACM Programming Competition

ACM sorting problems

11462 Age Sort

10810 Ultra-

QuickSort

612 DNA Sorting

1016 Silly Sort

10327 Flip Sort

12004 Bubble Sort

11456

Trainsorting

11321 Sort! Sort!! and Sort!!!

1232 Skyline

11297 Census

Data structures

Web:

http://www.people.vcu.edu/~tndinh/acm/prac3.html