Professional Documents
Culture Documents
#include<stdio.h>
#include<string.h>
void main()
{
char str[50],revstr[50];
int i=0,j=0;
clrscr();
printf("Enter the string to be reversed : ");
scanf("%s",str);
for(i=strlen(str)-1;i>=0;i--)
{
revstr[j]=str[i];
j++;
}
revstr[j]='\0';
printf("Input String : %s",str);
printf("\nOutput String : %s",revstr);
getch();
}
#include <stdio.h>
#include <stdlib.h>
/* Reverse the string and put the result into allocated memory. */
char * reverse (const char * string)
{
int length;
int j;
char * r;
/* Get the length of the string. */
for (length = 0; string[length]; length++)
;
/* Get memory. */
r = malloc (length + 1);
if (! r) {
fprintf (stderr, "Malloc failed.\n");
exit (EXIT_FAILURE);
}
/* Copy the bytes of the string. */
for (j = 0; j < length; j++) {
r[j] = string[length - j - 1];
}
r[length] = '\0';
return r;
}
***********************************************
#include <stdio.h>
main ()
{
int i=0,k=0,j;
char a[30],b[30];
printf("\nEnter the string:\n");
gets(a);
while(a[i++]!='\0')
k++;
for (i = 0,j=k-1; i <k,j>=0 ; i++,j--)
b[i] = a[j];
printf("%s",b);
return(0);
}
********************************************************************************
***************************
sorting in array in c
pair of elements and switching their positions if necessary. This process is rep
eated as many times as necessary, until the array is sorted. Since the worst cas
e scenario is that the array is in reverse order, and that the first element in
sorted array is the last element in the starting array, the most exchanges that
will be necessary is equal to the length of the array. Here is a simple example:
Given an array 23154 a bubble sort would lead to the following sequence of parti
ally sorted arrays: 21354, 21345, 12345. First the 1 and 3 would be compared and
switched, then the 4 and 5. On the next pass, the 1 and 2 would switch, and the
array would be in order.
The basic code for bubble sort looks like this, for sorting an integer array:
for(int x=0; x<n; x++)
{
for(int y=0; y<n-1; y++)
{
if(array[y]>array[y+1])
{
int temp = array[y+1];
array[y+1] = array[y];
array[y] = temp;
}
}
}
Notice that this will always loop n times from 0 to n, so the order of this algo
rithm is O(n^2). This is both the best and worst case scenario because the code
contains no way of determining if the array is already in order.
A better version of bubble sort, known as modified bubble sort, includes a flag
that is set if an exchange is made after an entire pass over the array. If no ex
change is made, then it should be clear that the array is already in order becau
se no two elements need to be switched. In that case, the sort should end. The n
ew best case order for this algorithm is O(n), as if the array is already sorted
, then no exchanges are made. You can figure out the code yourself! It only requ
ires a few changes to the original bubble sort.
Selection sort and insertion sort are two simple sorting algorithms; they are mo
re often efficient than bubble sort, though they aren't the top of the class alg
orithmically.
Selection sort
Selection sort is the most conceptually simple of all the sorting algorithms. It
works by selecting the smallest (or largest, if you want to sort from big to sm
all) element of the array and placing it at the head of the array. Then the proc
ess is repeated for the remainder of the array; the next largest element is sele
cted and put into the next slot, and so on down the line.
Because a selection sort looks at progressively smaller parts of the array each
time (as it knows to ignore the front of the array because it is already in orde
r), a selection sort is slightly faster than bubble sort, and can be better than
a modified bubble sort.
Here is the code for a simple selection sort:
for(int x=0; x<n; x++)
{
int index_of_min = x;
for(int y=x; y<n; y++)
{
if(array[index_of_min]>array[y])
{
index_of_min = y;
}
}
int temp = array[x];
array[x] = array[index_of_min];
array[index_of_min] = temp;
}
The first loop goes from 0 to n, and the second loop goes from x to n, so it goe
s from 0 to n, then from 1 to n, then from 2 to n and so on. The multiplication
works out so that the efficiency is n*(n/2), though the order is still O(n^2).
Insertion Sort
Insertion sort does exactly what you would expect: it inserts each element of th
e array into its proper position, leaving progressively larger stretches of the
array sorted. What this means in practice is that the sort iterates down an arra
y, and the part of the array already covered is in order; then, the current elem
ent of the array is inserted into the proper position at the head of the array,
and the rest of the elements are moved down, using the space just vacated by the
element inserted as the final space.
Here is an example: for sorting the array the array 52314 First, 2 is inserted b
efore 5, resulting in 25314 Then, 3 is inserted between 2 and 5, resulting in 23
514 Next, one is inserted at the start, 12354 Finally, 4 is inserted between 3 a
nd 5, 12345
***********