{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lec c5 - 1 Computer Programming I TA C162 Today’s Agenda...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Computer Programming I TA C162 . Today’s Agenda • Inserting an element in an Array. • Deleting an element from an Array. • Sorting a list of numbers (Insertion Sort) . • Finding maximum and minimum element in an Array. • Sorting a list of numbers (Selection Sort ). 2 Computer Programming I TA C162 . Example: Inserting an element at specified index in an Array. int main (void) { int arr[100]; int element, index, i; printf ("Enter the new element and index"); scanf("%d %d",&element, &index); // Assume that array elements are initialized. for (i=99 ; i>index ; i--) // Shift the elements arr[i] = arr[i-1]; arr[index] = element; } Q. How to delete an element at specified location ?? 3 Computer Programming I TA C162 . Sorting 4 Computer Programming I TA C162 . Sorting Input: A sequence of n numbers a 1 , …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’ 14 3 2 6 8 16 2 3 14 16 6 8 5 Computer Programming I TA C162 . Sorting Big idea: • Inserting an element into a sorted list in the appropriate position retains the order. • So what? Start with a singleton list – sorted trivially. Repeatedly insert elements – one at a time – while keeping it sorted. • Leads to sorting technique known as “Insertion Sort” 6 Computer Programming I TA C162 . Insertion sorting Algorithm Steps: Assumptions: arr: Array to be sorted arr_sort: Sorted Array • Copy first element of arr to sort_arrr....
View Full Document

{[ snackBarMessage ]}

Page1 / 22

Lec c5 - 1 Computer Programming I TA C162 Today’s Agenda...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online