Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.

Similar presentations


Presentation on theme: "Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These."— Presentation transcript:

1 Dr. Sajib Datta CSE@UTA

2  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

3  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

4 4 10 9 6 -1 Pick 4, compare with 10

5 4 10 9 6 -1 Pick 10, compare with 9 Need to swap

6 4 9 10 6 -1

7 4 9 10 6 -1 Pick 10, compare with 6 Need to swap

8 4 9 6 10 -1

9 4 9 6 10 -1 Pick 10, compare with -1

10 4 9 6 -1 10 Notice, that 10 is at the right position Now, repeat from the beginning

11 4 9 6 -1 10 Compare 1 st element with 2 nd element

12 4 9 6 -1 10 Compare 2nd element with 3rd element Swap needed

13 4 6 9 -1 10

14 4 6 9 -1 10 Compare 3 rd element with 4 th element Swap needed

15 4 6 -1 9 10

16 4 6 -1 9 10 Now, do we need the compare 4 th with the 5 th ? We already know 5 th element is the largest. This implies, what we have as the 4 th element, it must be the second largest, and is already in the correct place.

17 4 6 -1 9 10 Now, repeat from the beginning again. But remember, we only need to go up to 3 rd this time.

18  1 st iteration places the largest number in the correct place  2 nd iteration places the second largest number in the correct place  …..  ….  i-th iteration places the i-th largest number in the correct place

19 #include  #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;  }

20

21  A string is a series of one or more characters, terminated by a null character.  ◦Example  ◦“I am at UTA”  The double quotation marks are not part of the string.  They just inform the compiler they enclose a string,  just as single quotation marks identify a character.

22  C does not have a string type.  Characters in a string are stored in adjacent memory cells, one character per cell, and an array consists of adjacent memory locations, so placing a string in an array is quite natural.  We use an array of chars for storing a string.  We can declare this just like we did with other types.  Example  char letters[12] = “I am at UTA”; IamatUTA\0

23  \0 is the null character which is used by C for marking the end of a string  The null character is not the digit zero; it is the nonprinting character.  Its ASCII code value is 0.  The presence of the null character means the array must have at least one more cell than the number of the characters to be stored

24  Even though we define a char array much larger than a string, the null character will tell the compiler where is the end of the string. This is useful when we call printf( ).  char letters[15] = “I am at UTA”; IamatUTA\0

25  gets() – read a line from input  Reads characters from stdin and stores them as a string into str until a newline character ('\n').  The ending newline character ('\n') is not included in the string.  A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.  Notice that gets() does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid overflows.

26  puts(the name of your char array) print out a string.  #include  int main(void)  {  char myString[41];  //printf("Please intput a string with characters <= 40:\n");  puts("Please intput a string with characters <= 40:\n");  gets(myString);  puts(myString);  return 0;  }  Note that all input will be regarded as one string.  However, scanf(“%s”, myString) will only read the characters until the first space as one string.

27  Scan a string and print it out  #include  int main(void)  {  char name[40];  printf("What's your name?\n");  scanf("%s", name);  printf("Hello, %s.\n", name);  return 0;  }   %s is the format specifier for a string.  The name of a char array represents an address in computer memory, therefore, we don‟t need “&” when we call scanf function.

28  If your input is ◦ Sajib Datta  Just calling scanf() once will only store Sajib in the char array as a string.  The remaining of the input will be ignored.  In this case, the space is used to separate strings in a sentence.

29  Scan a string and print it out  #include  int main(void)  {  char name[40];  printf("What's your name?\n");  gets(name);  printf("Hello, %s.\n", name);  return 0;  }   If you input “Sajib Datta”, it will print “Hello Sajib Datta.”

30 1. char a[2] = {‘x’,’y’}; printf(“%s”, a); // puts(a); 2. char a[3] = {‘x’,’y’}; printf(“%s”,a); //puts(a); 3. char a[2] = “xy”; printf(“%s”,a); //puts(a); 4. char a[3] = “xy”; printf(“%s”,a); //puts(a); 5. char a[5] = {‘’x’,’y’,’z’,’\0’,’d’}; printf(“%s”,a); //puts(a);

31 6. char a[5] = “xyz\0d”; printf(“%s”,a); //puts(a); 7. char a[5] = “xy\\0”; printf(“%s”,a); //puts(a);


Download ppt "Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These."

Similar presentations


Ads by Google